0

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"}]
1

2 Answers 2

2

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"}]
Sign up to request clarification or add additional context in comments.

1 Comment

this's working, thank u so much!!!!
1
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.