【发布时间】:2020-07-07 14:03:27
【问题描述】:
我正在尝试获取名为平台的嵌套数组,但我只想要其中的第一个键。所以对于 Array 它应该是 [{platforms: [windows], [windows]]} 而不是 [{platforms: [windows, osx, linux, null], [windows, null, null, null]]} 这甚至可以实现吗?我查看了.map 和.filter,但似乎无法抓住数组的第一块。
数组示例
[{ id: 1,
game: { position: 1},
platforms: [ 'windows', 'osx', 'linux', null ],
title: 'xxx',
user: {
url: 'xxxx',
name: 'xxx',
id: 1
}
},{ id: 2,
game: { position: 2},
platforms: [ 'windows', null, null, null, ],
title: 'xxx',
user: {
url: 'xxxx',
name: 'xxx',
id: 2
}
]
如何在 Javascript / NodeJS 中处理这个问题
var result = body.games.filter(a=>a).reduce((acc, a) => {
return acc.concat(a)
}, []).map(a=>a.platforms);
console.log(result);
结果 =
[ 'windows', 'osx', 'linux' null ], [ 'windows', null, null, null ],
【问题讨论】:
标签: javascript node.js arrays json