尽量避免使用任何库,因为我只需要一个简单的脚本。我想从现有数组中获取不存在的记录。
input = [{name: 'james'}, {name: 'jane'}]
existing = [{name: 'james'}]
//做一点事
预期输入变为 [{name: 'jane'}]
我试过这个
let input = [{
name: 'yahoo.com',
},{
name: 'google.my',
}]
existing = (existing || []).map(o => ({name: o.name})) //clean up data from backend [{name:'google.my'}]
input = (input || []).map(o => o.name) //just in case input has more property in the future
input = existing.filter(o => !o.name.includes(input))
console.log(input)
不知何故,我仍然没有得到我想要的(期望输入是[{name: 'yahoo.com'}]
,缺少什么?我无法发现它。
您可以使用filter
和find
let input = [{name: 'james'}, {name: 'jane'}];
let existing = [{name: 'james'}];
let result = input.filter(v => !existing.find(o => o.name == v.name));
console.log(result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句