我試圖獲取包含許多對象的許多數組的數組,並將其操作爲對象數組。將許多數組合併成一個數組(JavaScript)
所以,讓我們說我有一個數組,看起來像這樣:
[
[
{Name: 'Josh', email: '[email protected]', Points: 33},
{Name: 'Doug', email: '[email protected]', Points: 12}
],
[
{Name: 'Josh', email: '[email protected]', Points: 11},
{Name: 'Doug', email: '[email protected]', Points: 18}
],
[
{Name: 'Josh', email: '[email protected]', Points: 2},
{Name: 'Doug', email: '[email protected]', Points: 27}
]
]
期望的結果將是對象的數組,有一個「點」屬性以保存點的數組。所以它看起來是這樣的:
[
{Name: 'Josh', email: '[email protected]', Points: [33, 11, 2]},
{Name: 'Doug', email: '[email protected]', Points: [12, 18, 27]}
]
這個問題看起來很簡單,但我似乎無法弄清楚這樣做的最佳方式。如果你想把這個數據放在小提琴裏,我就讓你玩這個:http://jsfiddle.net/Qhxzz/1/
是這些預定義或動態的結構嗎? – jbabey
你需要遞歸展平還是隻有一個層次?你使用的是underscore.js還是類似的? – singpolyma
結構是靜態的。我不需要遞歸拼合,並且我沒有使用任何js庫 – frshca