![](/img/trans.png)
[英]ES6 - Create new array from an array based on key name using javascript es6
[英]nested array object comparison with another array of elements and create new array using Javascript or ES6
我有一個復雜的數組,如下所示
sectionDetail = [{id: 1, name:'ma'}, {id: 2, name:'na'}, {id: 3, name:'ra'}, {id: 4, name:'ka'}, {id: 5, name:'pa'}];
abc = [{id:'1', name:'zam', sections:['1',4]}, {id:'2', name:'dam', sections:['3']}, {id:'3', name:'nam', sections:['2','4']}];
現在我必須遍歷abc
關於節以用它們各自的sectionDetail
值替換數組元素
我嘗試將它循環到一個新變量,但我的部分每次都被替換。 下面是我試過的代碼。
const matchingBoost = [];
const getCategoryBasedBoostList = [];
abc.forEach((item, i) => {
sectionDetail.forEach((val, index) => {
item.section.forEach((value, x) => {
if (value == val.Id) {
matchingBoost.push(val);
}
});
});
getCategoryBasedBoostList.push({
Name: item.Name,
Boost: matchingBoost
});
});
所以基本上我正在尋找一個像這樣的新數組
xyz = [{name:'zam', sections:[{id: 1, name:'ma'}, {id: 4, name:'ka'}]},
{name:'dam', sections:[{id: 3, name:'ra'}]}, {name:'nam', sections:[{id: 2, name:'na'}, {id: 4, name:'ka'}]}];
希望我說得通,並希望得到一些回應。
您可以使用Map
和 map 獲取帶有sectionDetail
項目的數據。
var sectionDetail = [{ id: 1, name: 'ma' }, { id: 2, name: 'na' }, { id: 3, name: 'ra' }, { id: 4, name: 'ka' }, { id: 5, name: 'pa' }], data = [{ id: '1', name: 'zam', sections: ['1', 4] }, { id: '2', name: 'dam', sections: ['3'] }, { id: '3', name: 'nam', sections: ['2', '4'] }], map = new Map(sectionDetail.map(o => [o.id, o])), result = data.map(({ name, sections }) => ({ name, sections: sections.map(id => map.get(+id)) }) ); console.log(result);
.as-console-wrapper { max-height: 100%;important: top; 0; }
您基本上可以根據里面的sectionDetail
是否包含在abc
的sections
中來過濾object.id
中的部分。 在這兩種情況下,我都將索引映射到數字,因為一種是字符串,另一種是 integer。
sectionDetail = [{id: 1, name:'ma'}, {id: 2, name:'na'}, {id: 3, name:'ra'}, {id: 4, name:'ka'}, {id: 5, name:'pa'}]; abc = [{id:'1', name:'zam', sections:['1',4]}, {id:'2', name:'dam', sections:['3']}, {id:'3', name:'nam', sections:['2','4']}]; xyz = abc.map(item => ({...item, sections: sectionDetail.filter(sect => item.sections.map(id => parseInt(id)).includes(parseInt(sect.id)))})); console.log(xyz);
所以你想從 abc 對象中刪除 id 並將 section 數組元素替換為相應的 details 對象? 這看起來像是 forEach 和 map 的工作。 我即將展示的代碼還對sections 數組進行了一些預處理,以使整體代碼更加高效。
const sections = sectionDetail.reduce((result, section) => {
result[section.id] = section;
return result;
}, {});
abc.forEach(item => {
delete item.id;
item.sections = item.sections.map(id => sections[id]);
});
試試這樣:
const sectionDetail = [
{ id: 1, name: 'ma' },
{ id: 2, name: 'na' },
{ id: 3, name: 'ra' },
{ id: 4, name: 'ka' },
{ id: 5, name: 'pa' }];
const abc = [
{ id: '1', name: 'zam', sections: ['1', 4] },
{ id: '2', name: 'dam', sections: ['3'] },
{ id: '3', name: 'nam', sections: ['2', '4'] }
];
const desired = abc.map(({id, name, sections}) => {
return {id, name, sections : sectionDetail.filter(f => {
return sections.map(s => +s).includes(f.id)
})};
})
console.log(desired);
其中+s
正在轉換為Number
類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.