I am dealing with a complicated array structure as shown below
sectionInfo = [{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']}];
My task is to iterate through the abc
array based on sections and replace the elements with corresponding values from sectionDetail
array.
I attempted to accomplish this by looping it into a new variable, but I encountered issues where my sections were being replaced every time. Below is the code snippet that I tried:
const matchingData = [];
const updatedSectionList = [];
abc.forEach((item, i) => {
sectionDetail.forEach((val, index) => {
item.section.forEach((value, x) => {
if (value == val.Id) {
matchingData.push(val);
}
});
});
updatedSectionList.push({
Name: item.Name,
Data: matchingData
});
});
Ultimately, I aim to generate a new array structured like this
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'}]}];
I hope this explanation makes sense and I look forward to any helpful responses.