創建通過數字的指數陣列我有一個數組沒有推入陣
var nums = [1,2,4];
而且我還有一個陣列坐滿了人
var people = [
{ name: 'Adam', email: '[email protected]', age: 12, country: 'United States' },
{ name: 'Amalie', email: '[email protected]', age: 12, country: 'Argentina' },
{ name: 'Estefanía', email: '[email protected]', age: 21, country: 'Argentina' },
{ name: 'Adrian', email: '[email protected]', age: 21, country: 'Ecuador' },
{ name: 'Wladimir', email: '[email protected]', age: 30, country: 'Ecuador' },
];
我希望創建一個基於關使用的變量nums
變量充當people
變量的索引。
// With the nums array I take each value and set it as the value of the new variable
// This is my expected output. Although this is line of code is not possible since the nums variable will be unique each time the code run.
var select_people = [people[1], people[2], people[4]];
我無法創建一個空數組,然後將每個元素都推入select_people
數組中,像這樣。
// This will not do for me
var select_people = [];
for(var i = 0; i < nums.length; i++) {
select_people.push(people[nums[i]])
}
我的問題是這樣的。如何編寫此代碼,以便我可以分配select_people
變量而不必將值推入數組中?
等等......到底爲什麼你不想把東西推到'select_people'數組中? – soktinpk 2014-10-29 02:06:20
你代碼中的'i'是'undefined'。 – undefined 2014-10-29 02:07:28
您的預期產出或用例是什麼? – charlietfl 2014-10-29 02:07:30