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"}]
-
Does this answer your question? How to filter an array from all elements of another arrayRamesh Reddy– Ramesh Reddy2021年12月28日 03:54:12 +00:00Commented Dec 28, 2021 at 3:54
2 Answers 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
Dae Hyeon Mun
5364 silver badges8 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Victor
this's working, thank u so much!!!!
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
Wang YinXing
2781 silver badge6 bronze badges
Comments
lang-js