0

I have two arrays with objects and I want to compare them so I can get the objects that are not in array one, and those not in array two.

For example, if I have following two arrays:

var arrayOne = [{ "name":"John", "age":30},{ "name":"John", "age":35},{ "name":"John", "age":35}];
var arrayTwo = [{ "name":"John", "age":35},{ "name":"John", "age":40}];

...I want to match in such a way so I can get two new arrays like ResultOne and ResultTwo:

var ResultOne = [{ "name":"John", "age":40}]; //JSON new in Array Two
var ResultTwo = [{ "name":"John", "age":35},{ "name":"John", "age":40}]; //JSON NOT IN array TWO but in Array One

I tried all Q&A suggested on Stack Overflow, but can't find a way to get the arrays when two objects are the same, like I have {"name":"John", "age":35} occuring two times in arrayOne.

trincot
  • 317,000
  • 35
  • 244
  • 286
jkumar
  • 32
  • 8

1 Answers1

0

You can use the following as base concept

JSON.stringify(obj)

Now using for loop (Or any relevant ways) get each object in the array and compare.

safwanmoha
  • 306
  • 1
  • 2
  • 11