【问题标题】:Convert an array of nested objects to another nested index object将嵌套对象数组转换为另一个嵌套索引对象
【发布时间】:2021-07-27 05:10:24
【问题描述】:

当我有一个类似对象的动态嵌套数组时,我正在解决一个问题。我必须捕获嵌套索引将其转换为嵌套对象。这是两级嵌套数组的示例,我编写了简单的代码将其转换为嵌套索引对象。我正在寻找迭代或递归方法来处理动态嵌套数组并转换为嵌套的类似索引对象:-

var arr= [
  {
    "index": "1",
    "subRows": [
      {
        "index": "2",
        "subRows": undefined
      },
      {
        "index": "3",
        "subRows": undefined
      }
    ]
  },
  {
    "index": "4",
    "subRows": [
      {
        "index": "5",
        "subRows": undefined
      }
    ]
  }
];

var obj={}
for(var i =0; i<arr.length; i++) {
  obj[i]={};
  if(arr[i].subRows) {
     for(var j=0; j<arr[i].subRows.length; j++) {
       obj[i][j] = {};
     }
  }
}

console.log(obj)

【问题讨论】:

  • 您的问题中没有 JSON,并且 there is no such thing as a JSON Object。只是数组和对象。
  • 看来你可能需要写一个递归函数。
  • 你能举例说明 obj 的结果吗?
  • @MotiKorets 的输出是这样的,你也可以运行这段代码。0: {0:{0:{},1:{}}, 1:{0:{}} }
  • 在问题中添加想要的输出会更容易理解

标签: javascript arrays recursion iteration


【解决方案1】:

您可以将递归调用的结果分配给父级。

const
    convert = array => array.reduce((o, { subRows = [] }, index) => {
        o[index] = convert(subRows);
        return o;
    }, {}),
    array = [{ index: "1", subRows: [{ index: "2", subRows: undefined }, { index: "3", subRows: undefined }] },{ index: "4", subRows: [{ index: "5", subRows: undefined }] }],
    result = convert(array);

console.log(result);

【讨论】:

  • 我不想使用那个索引,而是实际的索引。
  • 这看起来很棒。谢谢
  • 请尝试一下。它应该可以工作,因为它可以在没有指定的情况下用于第二级。
  • 我已验证,它适用于任何嵌套级别。谢谢
【解决方案2】:

一种简单的递归方法

var arr = [{
    "index": "1",
    "subRows": [{
        "index": "2",
        "subRows": undefined
      },
      {
        "index": "3",
        "subRows": undefined
      }
    ]
  },
  {
    "index": "4",
    "subRows": [{
      "index": "5",
      "subRows": undefined
    }]
  }
];


let obj = {
  ...arr.map(function convert(obj) {
    return { 
      ...obj.subRows?.map(convert)
    }
  })
};

console.log(obj)
.as-console-wrapper{top:0;max-height:100%!important}

【讨论】:

  • TIL ({ ...undefined }) 有效,但 [ ...undefined ] 无效
  • @Thankyou:如果[...undefined] 表现得像[...([])] 那样会很方便,但我想我明白为什么不这样做......参数性问题,一个不适用的问题在对象情况下。
【解决方案3】:

这是使用object-scan 的迭代解决方案。

// const objectScan = require('object-scan');

const myArr = [{ index: '1', subRows: [{ index: '2', subRows: undefined }, { index: '3', subRows: undefined }] }, { index: '4', subRows: [{ index: '5', subRows: undefined }] }];

const extract = (arr) => objectScan(['**(^subRows$)'], {
  reverse: false,
  useArraySelector: false,
  breakFn: ({ key, property, context }) => {
    if (!Number.isInteger(property)) {
      return;
    }
    context.length = Math.ceil(key.length / 2);
    const last = context[context.length - 1];
    last[property] = {};
    context.push(last[property]);
  }
})(arr, [{}])[0];

console.log(extract(myArr));
// => { '0': { '0': {}, '1': {} }, '1': { '0': {} } }
.as-console-wrapper {max-height: 100% !important; top: 0}
&lt;script src="https://bundle.run/object-scan@14.3.1"&gt;&lt;/script&gt;

免责声明:我是object-scan的作者

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2019-01-31
    相关资源
    最近更新 更多