Javascript 以角度深度复制对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14360401/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 16:30:14  来源:igfitidea点击:

Deep copying objects in angular?

javascriptangularjs

提问by Adelin

I wonder if there is away to avoid copying references to objects when you need to create a simple object which has an array of embedded objects.The situation is as follow: I have server which accepts a JSON and applies some logic then stores the object in DB. lets say my form is for saving teams in DB. The server accepts team as json. the team has an array of TeamMember objects, my form has a simple field to enter team member info and add it to team teamMembers array. Now here is the problem, when I add a team member to array list and want to add another team member when I type into the field the added member is changed also !. I know the reason

我想知道当您需要创建一个具有嵌入式对象数组的简单对象时,是否可以避免复制对对象的引用。情况如下:我有一个接受 JSON 并应用一些逻辑的服务器,然后将对象存储在D B。可以说我的表格是用于在 DB 中保存团队。服务器接受团队为 json。团队有一个 TeamMember 对象数组,我的表单有一个简单的字段来输入团队成员信息并将其添加到团队 teamMembers 数组中。现在问题来了,当我将一个团队成员添加到数组列表并想在我输入字段时添加另一个团队成员时,添加的成员也被更改了!。我知道原因

$scope.addTeamMember=function(teamMember){
   $scope.team.teamMembers.push(teamMember);
}

and it is because I put same reference into the teamMembers array so I have same object added several times. to avoid this I should create a new team member object, copy all teamMember properties and and add it the array.

这是因为我将相同的引用放入 teamMembers 数组中,所以我多次添加了相同的对象。为了避免这种情况,我应该创建一个新的团队成员对象,复制所有 teamMember 属性并将其添加到数组中。

 $scope.addTeamMember=function(teamMember){
       var newTeamMember; /*<--- copy teamMember */
       $scope.team.teamMembers.push(newTeamMember); /*and add newTeamMember*/
    }

回答by Ben Lesh

Your question says you want to "avoid deep copy", but I'm not sure that's accurate. It sounds like you just want to use angular.copy, because you need to create a copy of the team member and add that to the array:

你的问题说你想“避免深度复制”,但我不确定这是否准确。听起来您只想使用angular.copy,因为您需要创建团队成员的副本并将其添加到数组中:

$scope.addTeamMember = function(teamMember) {
   var newTeamMember = angular.copy(teamMember);
   $scope.team.teamMembers.push(newTeamMember);
};

回答by anandharshan

This is the best documentation available

这是最好的文档

https://docs.angularjs.org/api/ng/function/angular.copy

https://docs.angularjs.org/api/ng/function/angular.copy

there is a live example as well on the page which is self illustrative.

页面上还有一个活生生的例子,它是自我说明的。

回答by Noman Chali

I personally use this:

我个人使用这个:

    function copyObjToObj(source, destination) {
        if(!angular.equals(source,destination)){
            if (!!destination) 
                angular.copy(source, destination);
            else 
                destination = angular.copy(source);
        }
        return destination;
    }
var destination = copyObjToObj(sourceObj, destination);