Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare two json array with TypeScript?

Tags:

typescript

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.

like image 811
AngularOne Avatar asked Mar 18 '26 11:03

AngularOne


1 Answers

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
like image 61
rageit Avatar answered Mar 20 '26 05:03

rageit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!