我在一个像这样的对象里面有一个数组。
sourceSystemArray = [{
  "attId" : 2257,
  "attributeName" : "country",
  "attributeValues" : [ "AU", "KG", "IN", "AF" ]
}]使用输入字段,我为用户提供了添加新值的选项。现在,我想最后将使用ngModel获得的新输入值添加到attributeValues数组中。因此,假设用户进入一个新的国家,比如说新西兰。所以我想把NZ推到attributeValues,我的最后一个对象应该是这样的:
sourceSystemArray = [{
      "attId" : 2257,
      "attributeName" : "country",
      "attributeValues" : [ "AU", "KG", "IN", "AF","NZ" ]
    }]我试过用推法,但不起作用。有人能帮我弄清楚怎么做到吗。
发布于 2020-03-21 07:18:53
如果要添加此对象,可能会出现问题,因为它位于数组本身。下面是我将如何添加到attributeValues中。
let myarray = [{
  "attId" : 2257,
  "attributeName" : "country",
  "attributeValues" : [ "AU", "KG", "IN", "AF" ]
}]
myarray[0].attribute values.push('GB')或者,假设它不是数组中唯一的项。
let myarray = [{
  "attId" : 2257,
  "attributeName" : "country",
  "attributeValues" : [ "AU", "KG", "IN", "AF" ]
}]
myarray.find(item => item.attId === 2257)
  .attributeValues.push('GB')发布于 2020-03-21 07:14:36
由于sourceSystemArray是一个数组,请尝试如下
sourceSystemArra[0].attributeValues.push("NZ");发布于 2020-03-21 07:16:41
在html中,u部分应该同时发送attId和带有函数的新值。
然后在组件u中使用attId查找方法
function(attId,newValue){    
    this.sourceSystemArray.find(data=>data.attId ==attId).attributeValues.push(newValue);  
}https://stackoverflow.com/questions/60785292
复制相似问题