【问题标题】:How to add the new value and update the existing value in array of objects on button click in angularjs?如何在angularjs中单击按钮时添加新值并更新对象数组中的现有值?
【发布时间】:2016-10-21 08:50:29
【问题描述】:

我在单击按钮时将对象值插入到数组中,如果在单击同一按钮时数组中已经存在对象,我想更新这些对象的值。我怎样才能做到这一点 ? 我正在使用 push 函数在数组中添加值,如下所示:

$scope.demoarray = [];

$scope.demoarray.push({                     
                        sample key1: $scope.demovalue1,
                        sample key2: $scope.demovalue2     
                       });

【问题讨论】:

  • $scope.demoarray = {key:value} 更新值 $scope.demoarray[0].key = value$scope.demoarray[0]["key"]= value 女巫 [0] 是数组元素的索引
  • 您的共享代码不是很有帮助。你能展示更多你的逻辑或添加一个小提琴吗?
  • 我想更新现有记录(如果存在)并添加新记录,如果同一数组中不存在记录 $scope.demoarray = [];在这种情况下有效吗?如果我想把它放在同一个函数中,我更新记录的条件是什么? @MamdouhFreelancer
  • 您想如何查看记录是否存在?通过属性id 还是什么?
  • 实际上我已经尝试过了,但它每次都会创建新的数组记录。我做了如下的事情: for(var i = 0; i

标签: javascript jquery angularjs arrays


【解决方案1】:

考虑到如果有任何键匹配编辑该记录,请查看此代码 -

$scope.demoarray = [];
var index1=-1;
var index2=-1;
var recordToBeAdded:{
             key1: $scope.demovalue1,
             key2: $scope.demovalue2};
  if($scope.demoarray.lenght>0){
   index1 = $scope.demoarray.findIndex(x=>x.key1===recordToBeAdded.key1) //find index of 1st key
   index2 = $scope.demoarray.findIndex(x=>x.key1===recordToBeAdded.key2) // find index of 2nd key
 }

  if(index1==-1 && index2==-1) // add if record is new
    {
        $scope.demoarray.push(recordToBeAdded);
     }                     
  else if(index1!==-1) // add edit same record if found 1st key matched
 {
   $scope.demoarray[index1] = recordToBeAdded
 }
 else{ // edit record if 2nd key matched
     $scope.demoarray[index2] = recordToBeAdded
    }

【讨论】:

    【解决方案2】:
        $scope.demoarray = [];
    
        $scope.demoarray.push({                     
                                sample key1: $scope.demovalue1,
                                sample key2: $scope.demovalue2     
                               });
    
        if($scope.demoarray.length>0){
        //if there is elements of array
        //reset the array
        //$scope.demoarray = [];
        //or push
        //$scope.demoarray.push({...});
        //or update item by index
        //$scope.demoarray[1] = {"newKey","newValue"}
        }
    
        if($scope.demoarray[0]["sample key1"]){
        //if there is value of key exists in the first item of array
        //$scope.demoarray.push({...});
        //or
        //$scope.demoarray[0] = {};
        //or
        //delete $scope.demoarray[0]["sample key1"];
        //$scope.demoarray.splice(0,1);
        //$scope.demoarray[1]["sample key1"]//key of 2nd item
        //$scope.demoarray[2]["sample key1"]//key of 3nd
        }
    
        if($scope.demoarray[0]["sample key1"]=="specific"){
        //take any action
        }   
        //and you can loop over all items using for
       for(var i=0;i<$scope.demoarray.length;i++){
       //use any if statements above working with i as index
       for(var k in $scope.demoarray[i]){
       console.log(k +" : "+ $scope.demoarray[i][k]);
       }
       }
    

    【讨论】:

      猜你喜欢
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 2020-03-03
      • 2022-07-01
      • 1970-01-01
      • 2018-09-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多