简体   繁体   English

嵌套数组 object 与另一个元素数组比较并使用 Javascript 或 ES6 创建新数组

[英]nested array object comparison with another array of elements and create new array using Javascript or ES6

I have a complex array's like shown below我有一个复杂的数组,如下所示

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']}];

Now I have to loop through the abc with respect to sections to replace the array elements with their respective sectionDetail values现在我必须遍历abc关于节以用它们各自的sectionDetail值替换数组元素

I have tried by looping it to a new variable but my sections is getting replaced every time.我尝试将它循环到一个新变量,但我的部分每次都被替换。 below is the code i tried.下面是我试过的代码。

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
    });
  });

so basically I'm looking for a new array something 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'}]}];

hoping I made sense and hoping for some response.希望我说得通,并希望得到一些回应。

You could take a Map and then map the data with the items of sectionDetail .您可以使用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; }

You can basically filter the sections from sectionDetail based on whether the object.id inside it is included in the sections of abc .您基本上可以根据里面的sectionDetail是否包含在abcsections中来过滤object.id中的部分。 I have mapped the indexes to number in both cases since one was string and the other was integer.在这两种情况下,我都将索引映射到数字,因为一种是字符串,另一种是 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);

So you want to remove the id from the abc objects and replace the sections array elements with the corresponding details objects?所以你想从 abc 对象中删除 id 并将 section 数组元素替换为相应的 details 对象? This looks like a job for forEach and map.这看起来像是 forEach 和 map 的工作。 The code I'm about to show also does a little bit of pre-processing of the sections array to make the overall code more efficient.我即将展示的代码还对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]);
});

Try like this:试试这样:

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);

where +s is casting to Number type.其中+s正在转换为Number类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM