前言
之前用reduce写过一次,现在忘完了,而且reduce用的不熟很难写出第二遍了。
最近又有同样的需求,在使用ant-design-vue的table时,获取到的表格数据十分庞大,几十个字段我只需要其中几个,于是再次踏入同一条河流!
使用
对于一个数组对象类型的数据:
const dataList = [
{
name:'张晓宇',
age:'6',
tel:'13322221111',
},
{
name:'王大壮',
age:'5',
tel:'2331112414',
},
{
name:'田美丽',
age:'1',
tel:'131313',
},
{
name:'徐向前',
age:'3',
tel:'2312323123',
},
]
使用 map 对数组中对象进行批量处理,并返回一个新的数组。
const resultList = dataList.map(item => {
return {
nickName: item.name,
age:item.age + '岁',
}
})
console.log(resultList)
处理前
处理后
以上!