簡體   English   中英

如何使用Angular.js / javascript將JSON數組值替換為新值

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

我需要使用Angular.js / Javascript替換JSON數組值。 這是我的下面的代碼。

假設我向如下所示的對象推送了一些值。

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

在循環內部,我將一些值推入數組,並使用該值調用一個函數。 這是subcategory分配給null。

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

在以上部分中,我將subcategory值null替換為某個值,但未替換。 請幫我。

您正在將另一個值推入數組,而不是替換它,但是您還必須知道答案的索引。

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

您正在將一個新對象推送至days[index].answer ,您應在其中分配子類別。 為此,您需要向setSubcatag添加一個新參數。

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

在基地

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM