1

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)
4
  • You only have functions here in the code ..? Commented Jun 19, 2019 at 6:24
  • 1
    First deep copies an Object object then pushes, second copies an Object then pushes and third just pushes an Object. Commented Jun 19, 2019 at 6:25
  • @Teemu I only need difference between this method Commented Jun 19, 2019 at 6:25
  • 1
    @ritaj just to further what you said - the second is a shallow copy. Although depending on the content of the objects, you might get more differences between 1 and 2. Commented Jun 19, 2019 at 6:27

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Maybe he is just pushing an Object function to an array, who said anything about reassigning?
"Will push a deep copy of 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.
Of course circular references will fail @VLAZ - I forgot about the functions, I'll add that to my answer.
1
Array.push(Object.assign({}, JSON.parse(JSON.stringify(Object))))

It's having four different things -

  1. JSON.stringify - When sending data to a web server, the data has to be a string. So you basically convert your object into 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.
  2. JSON.parse - It's opposite of 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.
  3. Object.assign - method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object. Here you are copying your copied object to a empty object - {} with all the other own properties/ proto properties
  4. Array.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.

Comments

0
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.

1 Comment

"You are making string from Object with JSON.stringify()" more specifically, it's serialising into JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.