[英]array map, map array as a key of an array
我知道標題可能聽起來令人困惑,但是使用$ .each卻花了一個小時。 基本上我有2個數組
[{"section_name":"abc","id":1},{"section_name":"xyz","id":2}];
和[{"toy":"car","section_id":1},{"tool":"knife","section_id":1},{"weapons":"cutter","section_id":2}];
我如何將它們作為新的屬性密鑰放入另一個
[{
"section_name": "abc",
"id": 1,
"new_property_name": [{
"toy": "car"
}, {
"tool": "knife"
}]
}, {
"section_name": "xyz",
"id": 2,
"new_property_name": [{
"weapon": "cutter"
}]
}]
ES6解決方案:
const arr = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}];
const arr2 = [{"toy":"car","id":1},{"tool":"knife","id":1},{"weapons":"cutter","id":2}];
const res = arr.map((section,index) => {
section.new_property_name = arr2.filter(item => item.id === section.id);
return section;
});
編輯:就像在評論中提到的georg一樣,上面的解決方案實際上是對arr
變異,它修改了原始的arr
(如果您在映射它之后登錄了arr,您將看到它已更改,對arr
進行了變異並具有new_property_name
)。 它使.map()
變得毫無用處,簡單的forEach()
確實更合適並節省了一行。
arr.forEach(section => {
section.new_property_name = arr2.filter(item => item.id === section.id));
});
嘗試這個
var data1 = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}]; var data2 = [{"toy":"car","id":1},{"tool":"knife","id":1},{"weapons":"cutter","id":2}]; var map = {}; //first iterate data1 the create a map of all the objects by its ids data1.forEach( function( obj ){ map[ obj.id ] = obj }); //Iterate data2 and populate the new_property_name of all the ids data2.forEach( function(obj){ var id = obj.id; map[ id ].new_property_name = map[ id ].new_property_name || []; delete obj.id; map[ id ].new_property_name.push( obj ); }); //just get only the values from the map var output = Object.keys(map).map(function(key){ return map[ key ] }); console.log(output);
您可以使用ah哈希表進行查找,並構建一個新對象以插入到new_property_name
數組中。
var array1 = [{ "section_name": "abc", "id": 1 }, { "section_name": "xyz", "id": 2 }], array2 = [{ "toy": "car", "section_id": 1 }, { "tool": "knife", "section_id": 1 }, { "weapons": "cutter", "section_id": 2 }], hash = Object.create(null); array1.forEach(function (a) { a.new_property_name = []; hash[a.id] = a; }); array2.forEach(function (a) { hash[a.section_id].new_property_name.push(Object.keys(a).reduce(function (r, k) { if (k !== 'section_id') { r[k] = a[k]; } return r; }, {})); }); console.log(array1);
似乎通過使用Jquery $.merge()
函數,您可以實現所需的功能。 然后,我們還有concat
函數,可用於將一個數組與另一個數組合並。
使用Object.assign()
在您的情況下,您可以像Object.assign(array1[0], array2[0])
。 合並對象非常好,因此,您只需要在數組中合並對象即可。
代碼示例:
var objA = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}];
var objB = [{"toy":"car","section_id":1},{"tool":"knife","section_id":1},{"weapons":"cutter","section_id":2}];
var objC = Object.assign({},objA[0],objB[0]);
console.log(JSON.stringify(objC));// {"section_name":"abc","id":1,"toy":"car","section_id":1}
有關更多信息,您可以在這里參考: Object.assign()
var firstArray = [{"section_name":"abc","id":1},{"section_name":"xyz","id":2}], secondArray = [{"toy":"car","section_id":1},{"tool":"knife","section_id":1},{"weapons":"cutter","section_id":2}]; var hash = Object.create(null); firstArray.forEach(s => { hash[s.id] = s; s['new_property_name'] = []; }); secondArray.forEach(i => hash[i['section_id']]['new_property_name'].push(i)); console.log(firstArray);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.