与数组属性angular.extend /合并对象 [英] angular.extend/merge object with array property
本文介绍了与数组属性angular.extend /合并对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我有这些对象:
var first = {arr: ["foo"]};
var second = {arr: ["bar"]};
然后执行
angular.extend(first, second);
这将是状态:
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.
当我尝试
angular.merge(first,second);
然后什么也没有发生在 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?
推荐答案
您可以两个对象合并成新的对象:
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
这篇关于与数组属性angular.extend /合并对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文