简体   繁体   中英

How to replace JSON array value withe new one using Angular.js/javascript

I need to replace JSON array value using Angular.js/Javascript. this is my code below.

Suppose i pushed some value to an object like below.

for(var i=0;i<mondayarr.length;i++){
    $scope.days[0].answers.push({
                category:{'value':mondayarr[i].cat_id},
                subcategory: null,
                comment: response.data[i].comment,
  })
  $scope.setSubcatag(0);
}

inside the loop i pushed some value into the array and called a function with the value. this is the subcategory is assigned to null.

$scope.setSubcatag=function(index){
   $scope.days[index].answers.push({
            subcategory:{'value':2}
  })
}

In the above section i am replacing subcategory value null to some value but its not replacing. please help me.

You are pushing one more value to the array, instead replace it, but you have to know the answer's index as well.

$scope.setSubcatag=function(index)
{
   $scope.days[index].answers[whichAnswerIndex].subcategory = {'value':subcat_id};
}

you are pushing a new object to the days[index].answer where you should assign the subcategory. in order to do so you need to add a new argument to the setSubcatag.

$scope.setSubcatag=function(index,answerId)
{
   $scope.days[index].answers[answerId].subcategory = {'value':2};
}

and in the base

for(var i=0;i<mondayarr.length;i++){
    $scope.days[0].answers.push({
                category:{'value':mondayarr[i].cat_id},
                subcategory: null,
                comment: response.data[i].comment,
  })
  $scope.setSubcatag(0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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