简体   繁体   English

使用foreach循环替换数组中的现有对象

[英]Replacing existing objects in array using foreach loops javascript

I am working on array with javascript. 我正在使用javascript处理数组。 I am able to add the array by using push. 我可以通过使用push添加数组。 However, I am not sure how to edit an items. 但是,我不确定如何编辑项目。 As you can see in this link below , I have the push method for my add item, which is working fine. 正如您在下面的此链接中看到的那样,我有用于添加项的push方法,该方法工作正常。 But what should I do with my edit button if I want to change replacing the old item with the new one? 但是,如果要更改将旧项目替换为新项目,该如何使用我的编辑按钮? Do I need to use foreach to do it? 我需要使用foreach来做到吗? Help will be appreciated jsfiddle.net/hin123/tcVhN/127/ 帮助将不胜感激jsfiddle.net/hin123/tcVhN/127/

I am stuck with this part $scope.saveDetail = function (data) { //angular.foreach } 我被这部分卡住了$ scope.saveDetail = function(data){//angular.foreach}

您可以使用Array.prototype.splice()插入项目,例如。

    $scope.tempData.splice($scope.tempData.indexOf(oldData), 1, newData);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM