【问题标题】:js merge values of array of objects with same key in array [duplicate]js合并数组中具有相同键的对象数组的值[重复]
【发布时间】:2022-10-24 16:55:41
【问题描述】:

我有一组对象,我想将所有名称合并到一个带有一个衬里的数组中。

[{
   id:1,
   name: 'peter'
},
{
  id:2,
  name: 'john'
}]

预期输出:

['peter', 'john']

【问题讨论】:

  • 什么也没有“合并”在您的要求中。您只提取公共属性的值。
  • const arr2 = arr1.map(({name}) => name)
  • 到目前为止,您尝试过什么来自己解决这个问题?

标签: javascript arrays object


【解决方案1】:

您可以map 数组创建另一个具有名称列表的数组:

const people = [{
   id:1,
   name: 'peter'
},
{
  id:2,
  name: 'john'
}];

const names = people.map(({ name }) => name);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-08
    • 1970-01-01
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    相关资源
    最近更新 更多