如何使用Angular.js / javascript替换新的JSON数组值

时间:2022-04-30 11:26:40

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

我需要使用Angular.js / Javascript替换JSON数组值。这是我的代码如下。

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.

在循环内部,我将一些值推入数组并使用该值调用函数。这是子类别被赋值为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.

在上面的部分中,我将子类别值null替换为某个值,但不替换它。请帮帮我。

2 个解决方案

#1


1  

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.

您正在将新对象推送到日期[index] .answer,您应该在哪里分配子类别。为此,您需要向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);
}

#2


1  

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};
}

#1


1  

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.

您正在将新对象推送到日期[index] .answer,您应该在哪里分配子类别。为此,您需要向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);
}

#2


1  

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};
}