2

For example I have this dataInput json:

dataInput= [
    {text: 'text1'},
    {text: 'text2'},
    {text: 'text3'}
];

and I want to compare it with:

dataInputUpdated= [
  {text: 'text1', info: 'something'},
  {text: 'text2'},
  {text: 'text3'}
];

How to compare between them with Typescript? Is there any equal method? This is array of objects, so I wanna know its not equal on any change.

AngularOne
  • 2,760
  • 6
  • 32
  • 46
  • Is the order of the objects in the array always going to be the same? – rageit Feb 23 '17 at 20:06
  • Yes, the order is the same. The change will be something inside each object, for example change in value of property or added proprety – AngularOne Feb 23 '17 at 20:23
  • 1
    http://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects – y2bd Feb 24 '17 at 01:41

1 Answers1

0

You can use JSON.stringify method to do the comparison. Here is a sample:

let dataInput= [
    {text: 'text1'},
    {text: 'text2'},
    {text: 'text3'}
];

let dataInputUpdated= [
  {text: 'text1', info: 'something'},
  {text: 'text2'},
  {text: 'text3'}
];

let areSame: boolean = JSON.stringify(dataInput) === JSON.stringify(dataInputUpdated);

alert(areSame); // returns false
rageit
  • 3,513
  • 1
  • 26
  • 38
  • Would this return true if `info` wasn't there AND `dataInput` had text2 first and text1 second, where `dataInputUpdated` had text1 first and text2 second? – PhillipJacobs Jun 18 '23 at 09:07