2012-03-27 22 views
1

我有2個陣列具有相同的長度是這樣的:加入在對象2個陣列,重複鍵

a = [12,21,21,13,13,13,13,31]; 
b = [4,7,4,6,2,9,4,1]; 

第一陣列是關鍵和第二陣列中的值,但如果鍵被重複的值應該分組在對應的鍵中而不是被替換。 對象應該像這樣:

o = { 
    12: [4], 
    21: [7,4], 
    13: [6,2,9,4], 
    31: [1] 
} 

這裏是我的嘗試:

var o = {}; 
for (var index in a) { 
    o[a[index]] = []; 
    o[a[index]].push(b[index]); 
} 

回答

2

不要使用for..in遍歷數組(除非他們'稀疏陣列,你知道你在做什麼; details)。

除此之外,你是在正確的軌道上,但你必須檢查數組是否覆蓋之前已經存在。所以:

var o = {}, key, entry; 
for (index = 0; index < a.length; ++index) { 
    // Get the key 
    key = a[index]; 

    // Get the entry's array if it already exists 
    entry = o[key]; 
    if (!entry) { 
     // It doesn't exist, create it and remember it in the object 
     o[key] = entry = []; 
    } 

    // Put this value in it 
    entry.push(b[index]); 
} 

或者幾個小的優化:

var o = {}, key, entry, len; 
for (index = 0, len = a.length; index < len; ++index) { 
    // Get the key 
    key = a[index]; 

    // Get the entry's array if it already exists 
    entry = o[key]; 
    if (!entry) { 
     // It doesn't exist, create it and remember it in the object, 
     // including this value as we go 
     o[key] = [b[index]]; 
    } 
    else { 
     // Already existed, add this value to it 
     entry.push(b[index]); 
    } 
} 

如果您使用的是啓用ES5環境(或者您包括ES5墊片),你可以使用forEach

var o = {}; 
a.forEach(function(key, index) { 
    var entry; 

    // Get the entry's array if it already exists 
    entry = o[key]; 
    if (!entry) { 
     // It doesn't exist, create it and remember it in the object, 
     // including this value as we go 
     o[key] = [b[index]]; 
    } 
    else { 
     // Already existed, add this value to it 
     entry.push(b[index]); 
    } 
}); 
+0

非常具有說明性 – olanod 2012-03-27 16:23:52

4

試試這個:

var o = {}; 
for (var i = 0; i < b.length; i++) { 
    var key = a[i] + ''; 
    if (key in o) { 
     o[key].push(b[i]); 
    } 
    else { 
     o[key] = [b[i]]; 
    } 
} 
+0

+1尼斯功能 - 可讀和乾淨的(雖然我」 d使用'String(i)'進行類型轉換而不是'+';當然這是不必要的,但可以帶來**最小的**性能增益)。在某些引擎上檢查密鑰的速度會比檢索後檢查速度慢([test](http://jsperf.com/check-for-prop-or-check-after)) - 該測試假定平均分配找到並沒有找到密鑰),但擔心這會是一個過早的優化,除非出現真實的,觀察到的性能問題。 – 2012-03-27 16:42:21

3

循環中的第一行是對該槽中的任何現有數組進行截斷。嘗試僅僅宣佈一個新的數組,如果一個不存在的話:

var o = {}; 
for (var index = 0; index < a.length; index++) { 
    if(o[a[index]] == undefined) { 
     o[a[index]] = []; 
    } 
    o[a[index]].push(b[index]); 
} 
+0

爲了簡單起見,終於把這個寫成了4行 – olanod 2012-03-27 16:28:31

0

這裏是你可以從phpjs.com使用此

function array_combine (keys, values) { 
    // Creates an array by using the elements of the first parameter as keys and the elements of the second as the corresponding values 
    var new_array = {},  keycount = keys && keys.length, 
     i = 0; 

    // input sanitation 
    if (typeof keys !== 'object' || typeof values !== 'object' || // Only accept arrays or array-like objects typeof keycount !== 'number' || typeof values.length !== 'number' || !keycount) { // Require arrays to have a count 
     return false; 
    } 

    // number of elements does not match if (keycount != values.length) { 
     return false; 
    } 

    for (i = 0; i < keycount; i++) {  new_array[keys[i]] = values[i]; 
    } 

    return new_array; 
}