繁体   English   中英

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

[英]How to add new fields to an Array of Objects, or insert values from one array to another - JS

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

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”字段的值。

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

我试图将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 }
];

 function namesAsOrgs({ includeOpportunities, orgSubSidNames }) { return orgSubSidNames.map((name, i) => { return { label: name, value: `${i}`, selected: !includeOpportunities } }) } console.log(namesAsOrgs({ includeOpportunities: true, orgSubSidNames: ['a', 'b', 'c', 'd', 'f', 'g'] })) 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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