I tried to push a object into array. its successfully Inserted in array. what is difference between this three code.
Array.push(Object.assign({}, JSON.parse(JSON.stringify(Object))))
Array.push(Object.assign({},Object))
Array.push(Object)
I tried to push a object into array. its successfully Inserted in array. what is difference between this three code.
Array.push(Object.assign({}, JSON.parse(JSON.stringify(Object))))
Array.push(Object.assign({},Object))
Array.push(Object)
Firstly, this code may not work if you've redefined your global objects - Object.assign is a static method, but because you're treating Object as an actual object (an instance of Object) then it won't be accessible via that method.
The code will only work if you rename the variables - I've just lowercased them - or if you realise that you're pushing the Object function to the array.
The first line:
array.push(Object.assign({}, JSON.parse(JSON.stringify(object))));
Will push a deep copy of object to the array. Deep copy means that there are no references - the two are completely different objects. Modifying the original object will not modify the deep copy of object inside array. The functions and methods will be stripped, as will all undefined values. Circular references (like window.window.window...) will also fail (because you're attempting to place an infinite amount of characters inside a string - never a good idea
The second line:
array.push(Object.assign({}, object));
Will push a shallow copy of object to array. This means that the properties on the object itself will be copied by value, but properties inside the nested objects will be copied by reference.
The third line:
array.push(object);
Will push a reference to object into array. This means that if object is modified, the object inside array will also be modified.
object to the array" it will also prune stuff like functions and undefined properties. So it's not necessarily a perfect copy. It would also fail with circular references.Array.push(Object.assign({}, JSON.parse(JSON.stringify(Object))))
It's having four different things -
String/searializing. But here in your case you probably have a deeply nested object that is why you converting it to string/searializing it to clone it fully because objects are reference variable.stringify When receiving data from a web server, the data is always a string. And as you are using Object.assign method which required object you need to convert you cloned sting object back to object.object to a empty object - {} with all the other own properties/ proto propertiesArray.push - Finally you are pushing your fresh object into an array
Array.push(Object.assign({},Object))
Here you are copying the address of object to new empty object, which then you push to an array however this is reference so any change to object inside this array will change the original object because you copied the address of the object.
Array.push(Object)
In this you are simply pushing the object address directly to array and any change there will modify original object.
Array.push(Object.assign({}, JSON.parse(JSON.stringify(Object))))
For the above example,
-You are making string from Object with JSON.stringify() and
-Making a new object from that string with JSON.parse() and
-Again making a new object with Object.assign() by passing new empty object {}.
-Here you clone the Object to new object and pushing it to the array.
Array.push(Object.assign({},Object))
For the above example,
-You are making a new object with Object.assign() by passing new empty object {}.
-Here you clone the Object to new object and pushing it to the array
Array.push(Object)
For the above example,
-You are just pushing the object to the array.