在对象中展平多个嵌套数组

时间:2018-03-13 06:32:36

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

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

当前结构:对象数组

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,因为它是动态的,并且具有不同的长度和属性名称。一直试图想出这一天。

由于

1 个答案:

答案 0 :(得分:1)

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

我在这里使用{...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);