[英]angular js - copy object without their reference being copied
I need to make a copy of an array in javascript such that the addresses of both arrays should be different. 我需要在javascript中复制数组,以便两个数组的地址都不同。 how can it be done..
如何做呢..
I have tried 我努力了
$scope.users = data.users;
$scope.usersTemp = [];
$scope.usersTemp.push($scope.users );
and
$scope.usersTemp = $scope.usersTemp
Have you tried with angular.copy
? 您尝试过
angular.copy
吗? Something like : 就像是 :
$scope.usersTemp = angular.copy($scope.usersTemp)
https://docs.angularjs.org/api/ng/function/angular.copy
https://docs.angularjs.org/api/ng/function/angular.copy
您可以使用:
angular.copy ( myObjorArrayetc )
what you are trying to do seems to be creating a shallow copy of a JS array. 您正在尝试做的事情似乎是创建JS数组的浅表副本 。 If this is not your use case, please let me know.
如果这不是您的用例,请告诉我。
There are several ways to do this in JS 在JS中有几种方法可以做到这一点
const copy = [...original]; // es6 only, uses Symbol.iterator
const copy = original.slice(); // slices the array from index 0,
// returning a shallow copy
const copy = Array.from(original, val => val); // es6 only, takes an
// array-like object with .length property and a map function
const copy = original.map(x => x);
// call to Array.prototype.map with identity function.
With those you can have : 有了这些,您可以:
const a = [1, 2];
const b = // shallow copy with one of the methods
a.push(3); // and b is still [1, 2]
A quick note regarding the other answers : a rapid look at the angular docs here seems to indicate that angular.copy returns a deep copy . 关于其他答案的快速注释:快速浏览此处的angular文档似乎表明angular.copy返回了深层副本 。
It is really important to grasp the difference : a shallow copy will merely create a new object and put all the values inside it, whereas a deep copy will try to make a copy of each of those values. 掌握差异非常重要:浅表副本只会创建一个新对象并将所有值放入其中,而深表副本将尝试为每个值创建一个副本。 What it means is that as objects in JS are mutable, if you create a shallow copy, you still share all of its values with the other copies.
这意味着,由于JS中的对象是可变的,因此,如果创建浅表副本,您仍将与其他副本共享其所有值。 This is not the case with a deep copy.
深度复制不是这种情况。
eg : 例如:
const a1 = [{a: {b: 3}}];
const a2 = // shallow copy
(a1 === 2) // false
a1[0].a.b = 4 // reassign a prop of a value contained inside a1
(a1[0].a.b === 4) // true
If a deep copy had been made, a new object would have been made. 如果制作了深拷贝,则将制作一个新对象。
Conclusion : use what you need depending of your use case. 结论:根据用例使用所需的东西。 A shallow copy is quick to be made, but is subject to unwanted mutations, a deep copy is much more expensive to create but immune to mutations caused by share access.
浅表副本可以快速创建,但是容易发生不必要的突变,深表副本的创建成本更高,但不受共享访问引起的突变的影响。 As a side note, there of course an impact on GC of these two approach (meaning a shallow copy will not release the references to values contained in the original ).
附带说明一下,这两种方法当然会对GC产生影响(这意味着浅表复制不会释放对原始所包含值的引用)。
You can use 您可以使用
var copy = angular.copy(object); var copy = angular.copy(object);
It will make a exact replica of the object. 它将精确复制对象。
When using assignment operation in case of array both copy and orginal object bind the changes. 如果使用数组的赋值操作,则复制和原始对象都将绑定更改。 So use
angular.copy(oldArray, newArray)
. 因此,请使用
angular.copy(oldArray, newArray)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.