Javascript 更新过滤对象时防止更改原始数组中的对象

Javascript 更新过滤对象时防止更改原始数组中的对象,javascript,angularjs,filter,angularjs-scope,Javascript,Angularjs,Filter,Angularjs Scope,我有一个数组 $scope.items = [{id:1 , name:'john'},{id:2, name:'doe'}]; 现在我正在从数组中筛选对象 $scope.newItem = $filter('filter')($scope.items, {id: 1})[0]; 然后,我在更新表单中使用$scope.newItem,但当我键入字段时,原始数组$scope.items中的对象也在更改。谁能帮我做错事 更多描述 我正在做两个步骤。在第一步中,我获取所有项目并将其存储在$sco

我有一个数组

$scope.items = [{id:1 , name:'john'},{id:2, name:'doe'}]; 
现在我正在从数组中筛选对象

$scope.newItem = $filter('filter')($scope.items, {id: 1})[0];
然后,我在更新表单中使用$scope.newItem,但当我键入字段时,原始数组$scope.items中的对象也在更改。谁能帮我做错事

更多描述

我正在做两个步骤。在第一步中,我获取所有项目并将其存储在$scope.items中。然后在第二步中,单击项目列表中的编辑,我正在筛选该id的对象,这里让1

$scope.newItem = $filter('filter')($scope.items, {id: 1})[0]; 

然后在显示新项目后,我想更新它。当我在newitem中键入一些内容来更新字段时,原始数组中的原始对象也在更新…

您正在混淆面向对象的概念

实际使用“=”意味着更改$scope.items的属性将更改$scope.newItem的相应属性,反之亦然

如果不想更改原始数组,请使用以下更改

$scope.items = [{id:1 , name:'john'},{id:2, name:'doe'}]; 

var copiedOne=angular.copy($scope.items);
然后更新你的副本

$scope.newItem = $filter('filter')(copiedOne, {id: 1})[0];

我希望这对你有用。

记住,我们对你的所作所为一无所知。到目前为止,你的问题很难理解。