如何将新字段添加到对象数组,或将值从一个数组插入到另一个数组-JS

时间:2018-10-09 11:43:47

标签: javascript arrays

我有一个当前的对象数组,如下所示:

this.subsidiaryOptions = [
  { label: FILTER_LABELS.topLevelOrganization, value: '0', selected: !this.includeOpportunities },
  { label: FILTER_LABELS.currentOrganization, value: '1', selected: !this.includeOpportunities }
];

现在我有一个像这样的新数组-我正试图将这些字段添加为另一个数组中的标签,或者使该数组适应原始数组:

   this.engagementService.orgSubSidNames = 
   ['a', 'b', 'c', 'd', 'f', 'g']

我想让当前数组在其label字段中包含此新数组成员的每个成员,然后使每个值的“值”增加1,并添加所有具有“ selected”字段的值。 / p>

或将字段添加到此新数组中,该数组的大小不是静态的,否则它将更改。

我试图将push方法与这样的标签一起使用:

    this.engagementService.orgSubSidNames.push.apply(this.engagementService.orgSubSidNames, this.subsidiaryOptions);

但是无法使其正常工作,我打算在for循环中对其进行说唱以增加值,但在此推送尝试中出现错误。

预期结果:

 Either array   = [
  { label: a, value: '0', selected: !this.includeOpportunities },
  { label: b, value: '1', selected: !this.includeOpportunities },
  { label: c, value: '2', selected: !this.includeOpportunities },
  { label: d, value: '3', selected: !this.includeOpportunities },
  { label: f, value: '4', selected: !this.includeOpportunities },
  { label: g, value: '5', selected: !this.includeOpportunities }
];

1 个答案:

答案 0 :(得分:1)

arr2[j]