我有一个数组
[ {a: 1}, {b:2}, {c:3} ]
我想将此数组转换为如下对象:
{ a: 1, child: {b: 2, child: { c: 3 } } }
就是这个问题。
最佳答案
ES5 可以帮助您使用圆滑的 Array 原型(prototype)方法:
[{a:1}, {b:1}, {c:1}, {d:1}].reduceRight(function(child, parent) {
parent.child = child;
return parent;
});
这将完成工作。
关于javascript - 如何将数组转换为具有子对象的对象作为javascript上的下一个数组项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30193480/