【发布时间】:2020-10-24 17:41:51
【问题描述】:
我有一个列表数组
const List = [
{id:1, name:"jack", sex:"male", age:23},
{id:2, name:"marry", sex:"female", age:18},
{id:3, name:"paul", sex:"male", age:12},
{id:4, name:"katty", sex:"female", age:20}
]
我想将数组减少到只有 [23,18,12,20]
我尝试使用reduce和accumulative
const newList = List.reduce(
(acc, {age}) =>{
acc = age
return acc
},[]}
那么结果只显示最后一个年龄根本没有累积,不知道最好的方法是什么。
【问题讨论】:
-
使用 map() 会更简单...
newList = List.map(({age}) => age) -
你实际上可以使用
Array.prototype.map来做到这一点。
标签: javascript arrays reduce accumulate