【问题标题】:How to create nested objects by returning objects in array with reduce in js如何通过在js中使用reduce返回数组中的对象来创建嵌套对象
【发布时间】:2021-12-14 15:13:55
【问题描述】:

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

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

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

  Parent[Child]={}

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

【问题讨论】:

    标签: javascript vue.js vuejs2


    【解决方案1】:
    ['a', 'b', 'c'].reduceRight((nest, key) => ({ [key]: nest }), {});
    // { a: { b: { c: {} } } }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-18
      • 2021-05-02
      • 1970-01-01
      • 2021-07-08
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多