0

I'm aware I can use $.extend to merge to objects. What if I want to merge/update an array into an existing array, which is teh value of one of the properties of an object?

Obj = {id: 63, region: "place", shops: Array[246]}

$.extend(true, Obj.traits, amendedShopsArray) // Doesnt work

to return Obj = {id: 63, region: "place", amendedShopsArray: Array[246]

0

2 Answers 2

1

You can add a second key to the object with the same data, and then delete the one you do not want:

// shops mocked for simplicity
var obj = {id: 63, region: 'place', shops: [1, 2, 3]};

obj.amendedShopsArray = obj.shops;
delete obj.shops;

console.log(obj);

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

Comments

0

Since $.extend will not merge arrays, you can use Array.concat like this:

var newArray = [.....];
Obj.shops = Obj.shops.concat(newArray);

If you want to change the property name:

var newArray = [.....];
Obj.amendedShopsArray = Obj.shops.concat(newArray);
delete Obj.shops;

Comments

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.