【问题标题】:Flatten multiple nested arrays in object展平对象中的多个嵌套数组
【发布时间】:2018-03-13 06:32:35
【问题描述】:

如果已经解决,请标记为重复。

当前结构:对象数组

const arrObj = [
  {
    head: 0,
    child_0: [
      { field: 0 },
      { field: 1 }
    ],
    child_1: [
      { field: 3 },
      { field: 4 },
      { field: 5 },
      { field: 6 }
    ],
    child_2: [
      { field: 7 },
      { field: 8 }
    ]
  }
]

预期输出:

const newArrObj = [
  { head: 0, child_0: { field: 0 }, child_1: { field: 3 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 3 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 4 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 4 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 5 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 5 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 6 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 0 }, child_1: { field: 6 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 3 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 3 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 4 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 4 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 5 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 5 }, child_2: { field: 8 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 6 }, child_2: { field: 7 } },
  { head: 0, child_0: { field: 1 }, child_1: { field: 6 }, child_2: { field: 8 } },
]

请注意,数组字段可能超过 2 个,因为它是动态的并且具有不同的长度和属性名称。已经想了一天了。

谢谢

【问题讨论】:

  • 那里可以有多少个孩子(child0child1)?
  • @gurvinder372 它是动态的,所以我不知道,它可能是尽可能多的。
  • 如果孩子的数量是动态的,输出应该是什么样子?
  • @gurvinder372 就像输出中的那个一样,只要它看到一个数组字段
  • ...so on - 您已包含示例输入,请包含准确所需的输出...以及您尝试过的内容

标签: javascript arrays object data-structures recursive-datastructures


【解决方案1】:

您可以使用reduce 和嵌套的for 循环。

我在这里使用了 spread {...child_0[k1]} 来克隆对象,而不仅仅是通过引用指向。如果您只想分配(通过引用),您可以使用child_0[k1]

const arrObj = [{head: 0,child_0: [{ field: 0 },{ field: 1 }],child_1: [{ field: 3 },{ field: 4 },{ field: 5 }]}];
	
const newArrObj = arrObj.reduce((c,{ head, child_0, child_1 })=>{
   for ( var k1 in child_0 ) {
          for ( var k2 in child_1 ) {
               c.push({
                  head : head,
                  child_0 : {...child_0[k1]},
                  child_1 : {...child_1[k2]},
               });
          }
   }
   return c;
},[]);
  
console.log( newArrObj );

对于多个child_*,我在这篇文章中借用了permute() 函数。

const arrObj = [{head: 0,child_0: [{ field: 0 },{ field: 1 }],child_1: [{ field: 3 },{ field: 4 },{ field: 5 }],child_2: [{ field: 55 },{ field: 66 }]}];
const newArrObj = arrObj.reduce((c, {head,...child}) => {
  var keys = Object.keys(child);

  function permute(input) {
    var out = [];

    (function permute_r(input, current) {
      if (input.length === 0) {
        out.push(current);
        return;
      }

      var next = input.slice(1);

      for (var i = 0, n = input[0].length; i != n; ++i) {
        permute_r(next, current.concat([input[0][i]]));
      }
    }(input, []));

    return out;
  }

  permute(Object.values(child)).forEach((v, i) => {
    let t = {head: head};
    v.forEach((x, k) => {t[keys[k]] = {...x};});
    c.push(t);
  });

  return c;
}, []);

console.log(newArrObj);

【讨论】:

  • 对于child_2child_3、...?
猜你喜欢
  • 2021-01-14
  • 2021-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
  • 2016-05-18
  • 2021-05-14
相关资源
最近更新 更多