0

I am comparing two JSON objects and returning only the differences, and it works great. But I need a TRUE or FALSE answer also. Were any new items added to the final array?

Let's assume

result1 = existing vehicles in file

result2 = new vehicle data

Were there any new vehicles added to the file (array) ?

newItemsAdded = ????? // Your code here

const comparer = async function (result1, result2) {

  //Find values that are in result1 but not in result2
  var uniqueResultOne = result1.filter(function (obj) {
    return !result2.some(function (obj2) {
      return (obj.title == obj2.title && obj.price==obj2.price && obj.miles==obj2.miles);
    });
  });

  //Find values that are in result2 but not in result1
  var uniqueResultTwo = result2.filter(function (obj) {
    return !result1.some(function (obj2) {
      return (obj.title == obj2.title && obj.price==obj2.price && obj.miles==obj2.miles);
    });
  });

  // Combine the two arrays of unique entries
  var result = uniqueResultOne.concat(uniqueResultTwo);

  return result;
}
John S.
  • 504
  • 1
  • 3
  • 18
  • 2
    This doesn't really have anything to do with JSON. Sounds like you want to do a deep object comparison? – Brad May 07 '20 at 17:26
  • JSON is a *textual notation* for data exchange. [(More here.)](http://stackoverflow.com/a/2904181/157247) If you're dealing with JavaScript source code, and not dealing with a *string*, you're not dealing with JSON. – T.J. Crowder May 07 '20 at 17:27

1 Answers1

0

Try using lodash, very useful lib where this is already solved.

https://lodash.com/docs/#isEqual

var object = { 'a': 1 };
var other = { 'a': 1 };

_.isEqual(object, other);
// => true

object === other;
// => false 
Matúš Bartko
  • 2,425
  • 2
  • 30
  • 42