2014-10-29 116 views
1

創建通過數字的指數陣列我有一個數組沒有推入陣

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變量而不必將值推入數組中?

+0

等等......到底爲什麼你不想把東西推到'select_people'數組中? – soktinpk 2014-10-29 02:06:20

+0

你代碼中的'i'是'undefined'。 – undefined 2014-10-29 02:07:28

+0

您的預期產出或用例是什麼? – charlietfl 2014-10-29 02:07:30

回答

1

如果這是你想要簡潔,那麼你可以嘗試:

var selectPeople = people.filter(function(k, i) { return nums.indexOf(i) >= 0; }); 

同樣,你可以做(​​我其實更喜歡這個):

var selectPeople = nums.map(function(k) { return people[k]; }); 

注意:這隻適用於現代瀏覽器。

但是,我想不到很多情況下使用push不是最好的選擇。

如果它是一個命名衝突,你可以隨時將它包裝在一個臨時的功能(這在所有瀏覽器上運行):

var selectPeople = (function() { 
    var temp = []; 
    for (var i = 0; i < nums.length; ++i) temp.push(people[nums[i]]); 
    return temp; 
})(); 

這基本上消除了任何命名衝突(或者,例如衝突,其中selectPeople不是一個真正的數組,因爲它缺少push方法)。

+0

我會試試這個。給我一兩分鐘。 – jason328 2014-10-29 02:13:32

+0

它的工作原理!謝謝!!!!如果你想知道爲什麼推不是最好的選擇。我正在使用AngularJS Select2,並且在更新Select2輸入字段的ng模型時遇到問題。推到ng模型變量不會蹲下,所以我必須創建一個完整的新數組。 – jason328 2014-10-29 02:23:07

0

您還沒有初始化你i變量在你for循環:

這應該工作:

var select_people = []; 

for(var i = 0; i < nums.length; i++) { 
    select_people.push(people[nums[i]]) 
} 
+0

噢..所以你想在你的'select_people'變量中推送值,而不使用'Array.push'? – 2014-10-29 02:11:29

+0

不推。只需將'select_people'變量全部分配到一行,而不使用'Array.push'。對不起,如果我沒有解釋得好。我甚至不知道是否有可能。 – jason328 2014-10-29 02:13:11

0

另一種獲得相同結果的方法。

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' }, 
 
]; 
 
var nums = [1,2,4]; 
 
var j = []; 
 
for(var i = 0, l = nums.length; i < l; i++) { 
 
    j.push(JSON.stringify(people[nums[i]])); 
 
} 
 
j = '[' + j.join(',') + ']'; 
 
var selectPeople = JSON.parse(j); 
 
console.log(selectPeople);

0
for(x=0;x<nums.length;x++){ 
    alert(people[nums[x]]['name']); 
    // or you can define your select_people here with people[nums[x]] 
    // yes, you get people[1], people[2] and people[4] 
    // but, your first people is "Adam", and people[1] is "Amalie" 
} 

,所以如果你想採取第一人看重 「NUMS」, 「1」,只是做

for(x=0;x<nums.length;x++){ 
    alert(people[nums[x]-1]['name']); 
    // or you can define your select_people here with people[nums[x]-1] 
    // yes, you get people[0], people[1] and people[3] 
    // your first people is "Adam", and people[0] is "Adam" 
}