我有這個數組:如何基於另一個對象數組創建一個對象數組?
var arr1 = [
{"user":"dan","liked":"yes","age":"22"},
{"user":"sarah","liked":"no","age":"21"},
{"user":"john","liked":"yes","age":"23"},
];
我想創建數組的新(子)陣列,只包含用戶的喜歡。
所以它看起來像這樣:
var arr2 = [
{"dan":"yes"},
{"sarah":"no"},
{"john":"yes"},
];
我想:
var arr2 =[];
for(var i in arr1){
arr2.push({[i[user]]:i[liked]});
}
它需要一個調整, 想法?
我在我的問題犯了一個錯誤!編輯!對不起 – Ted