【发布时间】:2017-11-02 09:49:46
【问题描述】:
我有一个来自数据库结果的类似数组的结构
var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}, {name: 'c', age: 35}]
我想创建一个新对象,其值为键和值,如下所示:
var new_arra = {a: 23, b: 24, c: 35}
如何在 lodash 中做到这一点?
我尝试了以下方法:
var result = _.forEach(results, function(index) {
var result = _.map(index, function(value, prop) {
return {prop: prop, value: value};
});
});
【问题讨论】:
-
只使用地图! const newResults results = results.map(item => ({ [item.name]: item.age }));
-
如果要从
Array转到Object,则需要使用reduce。
标签: javascript node.js lodash