从嵌套对象数组中移除多个对象



如何从嵌套对象数组中删除多个对象?我尝试下面的代码使用过滤器,但这是返回未定义。请建议一个更好的方法。

let removedIds = [1, 2]
let gridData = [{
Id: 1,
name: "ABC",
class: "XYZ",
college: "AB",
collectionRows: [{
Id: 1,
name: "ABC",
class: "XYZ",
college: "AB",
},
{
Id: 2,
name: "ABC",
class: "XYZ",
college: "AB",
},
{
Id: 3,
name: "ABC",
class: "XYZ",
college: "AB",
}
]
}];
let newData = gridData.map((row) => {
{
row.collectionRows = row.collectionRows.filter((subRow) => {
return !removedIds.includes(subRow.Id);
});
}
return row;
});
console.log(newData, 'newData');

您可以通过使用Array.filter()方法来实现这一点。

Live Demo:

let removedIds = [1, 2]
let gridData = [{
Id: 1,
name: "ABC",
class: "XYZ",
college: "AB",
collectionRows: [{
Id: 1,
name: "ABC",
class: "XYZ",
college: "AB",
},
{
Id: 2,
name: "ABC",
class: "XYZ",
college: "AB",
},
{
Id: 3,
name: "ABC",
class: "XYZ",
college: "AB",
}
]
}];
const res = gridData.map(obj => {
obj.collectionRows = obj.collectionRows.filter(({ Id }) => !removedIds.includes(Id));
return obj;
});
console.log(res);

相关内容

  • 没有找到相关文章

最新更新