1

So, and as is shown in the attached image, I have two objects, addedBack and Comments. What I want to do is overwrite the array of comments in addedBack, with the array comments in Comments.

I attempted:

const tempVal = Object.assign(addedBack, Comments)

But the value of tempVal just contained addedBack, without merging Comments into it.

How do I do this?

enter image description here

1
  • If it is as you said, then Object.assign is broken, which is highly unlikely. Can you provide minimal reproducible example and an example of the input and the expected output? Thank you! Commented Apr 28, 2017 at 15:56

4 Answers 4

2

Your approach should work:

var addedBack = { comments: ['that sucks', 'terrible', '0 stars', "It doesn't matter--we'll all still die in the end"]}; var Comments = { comments: ['fantastic', '5 stars', 'I love you', 'A stunning achievement']}; console.log(addedBack.comments); // <= ["that sucks", "terrible", "0 stars", "It doesn't matter--we'll all still die in the end"] Object.assign(addedBack, Comments); // Could also just do "addedBack.comments = Comments.comments;" if you only care about this one property console.log(addedBack.comments); // <= ["fantastic", "5 stars", "I love you", "A stunning achievement"] // Changing Comments.comments also changes addedBack.comments Comments.comments[0] = 'If I had a tomato, you would be covered in ketchup right now.'; console.log(addedBack.comments); // <= ["If I had a tomato, you would be covered in ketchup right now.", "5 stars", "I love you", "A stunning achievement"]

Note, however, that Object.assign makes a shallow copy of object properties. Comments.comments is a reference to an array, and that reference gets copied to addedBack.comments. After Object.assign, then both Comments.comments and addedBack.comments refer to the same array. As a result, if you change Comments.comments, you will also change addedBack.comments, as shown in the example above. If this behavior will be problematic for you, then your best option is probably:

addedBack.comments = Comments.comments.slice(); 

instead of Object.assign. slice() on an array will return a copy of the array.

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

Comments

0

Since this is a simple data structure (array), you can just do something like this:

addedBack.comments = JSON.parse(JSON.stringify(Comments.comments)); 

This might not work in all cases but it will work here to make a shallow copy.

This is another good stackoverflow thread about cloning.

1 Comment

This makes a deep copy of comments, not a shallow copy.
0

Here's another one for you.

If you use Object.assign, it will return an object with the properties of the first object overwritten by the second.

let obj1 = { x : [ '1', '2', '3' ] }; let obj2 = { x : [ 'a', 'b', 'c' ] }; let obj3 = Object.assign ( obj1, obj2 ); 

That third object will contain:

{ x : [ 'a', 'b', 'c' ] } 

It makes sense: "assign the properties/values of obj2, to obj1". Exactly as if you did this manually, it'll just overwrite the existing prop/val.

This also has the benefit of leaving the originals intact, say, if you wanted to test the property is actually different from the original or some such.

I mean...TESTING! (Wow look I have more credibility now).

(Object.assign is not broken).

Comments

0

This final solution was to import update from 'immutability-helper'.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.