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"}]
asked Dec 28, 2021 at 3:52
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"}]
answered Dec 28, 2021 at 4:04
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);
answered Dec 28, 2021 at 4:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.