我有一个如下数组
[{
"first_name": "Anna",
"last_name": "William",
"class": "math",
"year": "1990"
},
{
"first_name": "Tom",
"last_name": "Cruise",
"class": "biology",
"year": "1991"
}
]
我正在尝试将其转换为新数组
[{
"name": "Anna William",
"class": "math"
},
{
"name": "Tom Cruise",
"class": "biology"
}
]
我的简单解决方案显然是使用循环来创建一个新数组,但是它很长而且不够优雅:P。 Lodash使我的代码简短易读,对我有很大帮助。关于使用Lodash功能转换数组的任何建议,请与我分享。
答案 0 :(得分:0)
您只能使用.map
将其映射到具有所需值的新数组
let arr = [{"first_name": "Anna","last_name": "William","class": "math","year": "1990"},{"first_name": "Tom","last_name": "Cruise","class": "biology","year": "1991"}]
let op = arr.map(e => ({name: `${e.first_name} ${ e.last_name}`, class:e.class }))
console.log(op)