从另一个数组中存在的对象数组中删除对象

时间:2019-07-14 15:09:32

标签: javascript arrays

let existing_array  = [
    {name: "a",  age: 15},
    {name: "b",  age: 16},
    {name: "c",  age: 17},
    {name: "d",  age: 18}
    {name: "e",  age: 19}];

现在我有一个新数组

let new_array =  = [
    {name: "f",  age: 15},
    {name: "b",  age: 16},
    {name: "g",  age: 17},
    {name: "h",  age: 18}
    {name: "i",  age: 19}];

现在existing_array和new_arrray都具有名称值'b',我希望将其从新数组中删除

1 个答案:

答案 0 :(得分:1)

filter()find结合使用:

new_array = new_array.filter(x => !existing_array.find(y => x.age === y.age && x.name === y.age))

您可能想使用更好的相等性检查。