Home > database >  Remove object in array
Remove object in array

Time:12-28

I have a array object and a array.

const arr1=[
    { name: "viet" },
    { name: "hai" },
    { name: "han" }
    ]
const arr2= ["viet", "hai"];

How can i compare and set arr like:

arr = [{name: "han"}]

CodePudding user response:

Try this code :D

const arr1=[
    { name: "viet" },
    { name: "hai" },
    { name: "han" }
    ]
const arr2= ["viet", "hai"];
const result = arr1.filter(item => !arr2.includes(item.name))

console.log(result) // [{name: "han"}]

CodePudding user response:

const arr1=[
    { name: "viet" },
    { name: "hai" },
    { name: "han" }
    ]
const arr2= ["viet", "hai"];
let res = arr1.filter(function (n) {
                    return !this.has(n.name);
                }, new Set(arr2));
                
console.log(res);
  • Related