如何通过在js中使用reduce返回数组中的对象来创建嵌套对象

How to create nested objects by returning objects in array with reduce in js

我有一个数组,如何通过嵌套这个数组的每个元素在另一个对象中创建一个新对象

let newArr = ['Parent', 'Child']

我想将这个数组转换成这种格式

  Parent[Child]={}

我应该可以循环这个数组并根据数组的长度格式化它

['a', 'b', 'c'].reduceRight((nest, key) => ({ [key]: nest }), {});
// { a: { b: { c: {} } } }