繁体   English   中英

嵌套数组 object 与另一个元素数组比较并使用 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是否包含在abcsections中来过滤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.

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