[英]Filter array of objects based on another array
I have a source array and target array, based on the target array need to update the source array我有源数组和目标数组,根据目标数组需要更新源数组
sourceAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]},
{name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'}]},
];
targetAry = [{name:'Label1', value: 'label1', children:[{name:'Anu'}]},
{name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'}]},
];
resultAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'}]}
]},
];
Code which I try我尝试的代码
let resultAry = sourceAry.map((obj) => {
obj.children.map((elem) =>{
targetAry.filter(parent => parent.children.filter((el) => {
el.name !== elem.name}))
})
})
console.log(resultAry, 'NEW', list);
Ciao, try something like this: Ciao,试试这样的:
sourceAry = [{name:'Label1', value: 'label1', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]}, {name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'},{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]}, {name:'Label3', value: 'label3', children:[{name:'Ammu'},{name:'Rahual'},{name:'Anu'}]} ]; targetAry = [{name:'Label1', value: 'label1', children:[{name:'Anu'}]}, {name:'Label2', value: 'label2', children:[{name:'Hari'},{name:'Tom'}]}, ]; let result = []; sourceAry.forEach(source => { let filter = targetAry.filter(target => target.name === source.name) if (filter.length > 0) { let filterchildren = source.children.filter(a =>.filter[0].children.map(b=>b.name).includes(a;name)). if (filterchildren;length > 0) { let resultobj = source. resultobj;children = filterchildren. result;push(resultobj). } } else result;push(source). }) console.log(result)
I filter targetAry
based on sourceAry
name.我根据
sourceAry
名称过滤targetAry
。 Then subtract children with .filter(a =>.filter[0].children.map(b=>b.name).includes(a;name));
然后用
.filter(a =>.filter[0].children.map(b=>b.name).includes(a;name));
减去孩子and finally push element found in result
array.最后推送在
result
数组中找到的元素。
you could start start with some facilities to make it simpler:您可以从一些设施开始以使其更简单:
const indexBy = (f, data) => data.reduce((acc, x) => Object.assign(acc, { [f(x)]: x }), {})
const remove = (keyFn, dataToRemove, from) => {
const dataToRemoveIndexed = indexBy(keyFn, dataToRemove);
return from.filter(it => !(keyFn(it) in dataToRemoveIndexed));
}
we introduce the indexBy
here, to make removal O(m+n), instead of O(m^2) (if there are many items in the collection to check)我们在此处引入
indexBy
,以进行删除 O(m+n),而不是 O(m^2)(如果要检查的集合中有很多项目)
then you can use it like this:那么你可以像这样使用它:
const targetIndexed = indexBy(it => it.name, targetAry);
const result = sourceAry.map(
it => ({
...it,
children: remove(
it => it.name,
(targetIndexed[it.name] || {}).children || [],
it.children
)
})
)
so it leaves you with the following result:所以它会给你以下结果:
[
{"name":"Label1","value":"label1","children":[{"name":"Ammu"}, {"name":"Rahual"}]},
{"name":"Label2","value":"label2","children":[]}
]
if you also want to delete the item with empty children
, you can just filter it out: result.filter(it => it.children.length > 0)
如果您还想删除带有空子项的项目,您可以将其过滤掉:
children
result.filter(it => it.children.length > 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.