Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Javascript: how to compare two arrays of objects and remove equal values?

I have two arrays:
This is the main array(im printing it in html but the user can remove objects):

checkboxes = [
{name: "boxes", color: "red"},
{name: "boxes", color: "orange"},
{name: "tree", color: "green"}
{name: "tree", color: "red"}
{name: "tree", color: "yellow"}
]

And this is the array with the items to remove in checkboxes[]:

finalArray = [
{name: "tree", color: "green"}
{name: "tree", color: "red"}
{name: "tree", color: "yellow"}
]

The result after removing equal objects from those array must be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

checkboxes = [
{name: "boxes", color: "red"},
{name: "boxes", color: "orange"}
]

So im receiving an array (finalArray[]) with the objects to remove in the checkboxes[].
All equal objects must be removed.
I tried:
.filter and the .map but i didnt get a progress.

Thanks.

>Solution :

let checkboxes = [
  {name: "boxes", color: "red"},
  {name: "boxes", color: "orange"},
  {name: "tree", color: "green"},
  {name: "tree", color: "red"},
  {name: "tree", color: "yellow"}
]
let finalArray = [
  {name: "tree", color: "green"},
  {name: "tree", color: "red"},
  {name: "tree", color: "yellow"}
]
checkboxes = checkboxes.filter(ele => !finalArray.some(finalEle => JSON.stringify(finalEle) === JSON.stringify(ele)))
console.log(checkboxes)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading