0

I have a project when I get some data from a route in node and I need to compare with the last data I received to see if something changed, basically 2 arrays of objects. For the comparasion I used JSON.stringify(array1) = JSON.stringify(array2), but after I compare the arrays I want to put the value of array2 in array1, so after I receive one more value for array2 in the request, to be able to comparte with the last value witch is stored in array1. The structure of function I thought it is going to work is something like that

if(JSON.stringify(array1) != JSON.stringify(array2)) {
  console.log("not equal...");
  array1 = array2;
}

In array2 is what I receive on server, and after I compare their stringfy value, if they are not equal. change the value of array1 to exactly array2. After some research I understood that the array1 = array2 part it's the problem, but I can't figure out how to put the value of array2, which is an array of Objects, in array1. The structure of array2 is something like that

  [{
   data1: value,
   data2: value

  }
  { data1: value,
    data2: value,
  }
  { data1: value,
    data2: value 
  }]

1 Answers1

0

Using the = operator simply creates a pointer to the original array as it is a mutable object. Change a value on that, and both variables will see it. There are several common ways to create a new array from an existing one:

const array1 = [...array2];

const array1 = array2.slice();

const array1 = [].concat(array2);

Or, you could use:

const array1 = JSON.parse(JSON.stringify(array2));

This will convert nested arrays etc into strings and back into a new array.

ATD
  • 1,344
  • 1
  • 4
  • 8
  • It should be noted that the nested objects will still be references to the objects in `array2` and not clones so changes to values in the objects will still be seen in both `array1` and `array2` – pilchard Oct 01 '20 at 10:26
  • @pilchard Yep, sorry, forgot about that. I've added a JSON version which may handle more complicated arrays. But, presumably, only if a prototype object can be converted into a string - though a custom object can have a "toString" function defined, I'm not clear if JSON uses this? – ATD Oct 01 '20 at 10:46
  • So long as the objects don't themselves contain nested objects you could `array1 = array2.map(o => ({...o}));` but beyond that one strays into the complexities of deep copying [What is the most efficient way to deep clone an object in JavaScript?](https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript/5344074#5344074) – pilchard Oct 01 '20 at 10:54
  • @pilchard OMG, that's the page that I still have bookmarked from way back when and where I got the JSON idea from in the first place. I've never used nested objects to such an extent that JSON couldn't handle it. Any ideas about custom "toString" functions and JSON? – ATD Oct 01 '20 at 11:01
  • i found it's easier to just compare the stringfy value of the array2 with array1, which now is a string, and if they are not equal just assign the stringfy value of array2 to array1. I know it's sound wrong, but for my project it's ok. Thx anyway to you guys! – Tudor Stanciulescu Oct 01 '20 at 12:04