我有一个像[ {a: 1}, {b:2}, {c:3} ]
我想将此数组转换为类似的对象:{ a: 1, child: {b: 2, child: { c: 3 } } }
这就是问题所在。
最佳答案
ES5可以帮助您使用流畅的Array原型方法:
[{a:1}, {b:1}, {c:1}, {d:1}].reduceRight(function(child, parent) {
parent.child = child;
return parent;
});
这将完成工作。