【问题标题】:how to extract array type in an array object in javascript [duplicate]如何在javascript中的数组对象中提取数组类型[重复]
【发布时间】:2019-12-03 11:59:12
【问题描述】:

如何过滤名称,只将数组类型保留在 newArray 中?

let data = [
  {name: 'Andy', details: [{age: 20, sex: "male"}]},
  {name: 'John', details: [{age: 25, sex: "male"}]},
];

//Output result
let newArray = [{age:20, sex: "male"}, {age:25, sex: "male"}];

我尝试完成详细信息.. 它如何提供帮助。 =)

【问题讨论】:

  • 预期结果是什么?到目前为止,您尝试过什么?

标签: javascript filter


【解决方案1】:

你可以像这样使用reduce

let result = data.reduce((acc, curr) => [...acc, ...curr.details],[])

【讨论】:

  • 酷。它解决了我的问题。
  • 很高兴,它可以提供帮助。选择它作为答案:)
【解决方案2】:

我不太明白你的问题。尝试使用 ES5 map 方法:

let data = [
    {name: 'Andy', details: [{age: 20, sex: male}]},
    {name: 'John', details: [{age: 25, sex: male}]},
];

let newArray = data.map(value=> value.details[0])
// newArray is [{age: 20, sex: male}, {age: 25, sex: male}]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-05-11
    • 2021-09-14
    • 2012-08-29
    • 1970-01-01
    • 2011-10-29
    • 1970-01-01
    • 2020-05-20
    • 2020-06-08
    相关资源
    最近更新 更多