1

If I have these objects:

var first = {arr: ["foo"]};
var second = {arr: ["bar"]};

and then do

angular.extend(first, second);

this will be the state:

first.arr[0] //"bar"
first.arr === second.arr;//true

I want the first part, but i don't want the arrays to be the same reference.

When i try

angular.merge(first,second);

then nothing happens to the first.arr

first.arr[0]; //"foo

What is the proper way to extend first with the properties of second including arrays, but not having the same references?

1 Answer 1

1

You can merge both objects into new object:

angular.extend({}, first, second);

Then it should be what you want:

var first = {arr: ["foo"]};
var second = {arr: ["bar"]};

var result = angular.extend({}, first, second);

console.log(result.arr[0]); // bar
console.log(first.arr === second.arr); // false
Sign up to request clarification or add additional context in comments.

1 Comment

The thing is first is a value service I use in various places so I need to update first.arr. Is angular.copy something I can use?

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.