How to get the combination of array values from nested arrays in an array of objects

Iss*_*aki 3 javascript arrays math combinations vue.js

I have an array of objects with the following structure:

 var varientSections = [
  {
    type: "frame",
    values: ["black", "white", "wood"]
  },
  {
    type: "finish",
    values: ["matte", "glossy"]
  }
];
Run Code Online (Sandbox Code Playgroud)

I want to get the combination of the array values and create a new list with it. Right now, I am able to retrieve the combination from the nested array values using the method called getCombination(varientSections). However, I do not know how to create a new list with the following structure:

var results = [
  {
    attributes: [
      {
        type: "frame",
        value: "black"
      },
      {
        type: "finish",
        value: "matte"
      }
    ]
  },
  {
    attributes: [
      {
        type: "frame",
        value: "black"
      },
      {
        type: "finish",
        value: "glossy"
      }
    ]
  },
  {
    attributes: [
      {
        type: "frame",
        value: "white"
      },
      {
        type: "finish",
        value: "matte"
      }
    ]
  },
  {
    attributes: [
      {
        type: "frame",
        value: "white"
      },
      {
        type: "finish",
        value: "glossy"
      }
    ]
  },
  {
    attributes: [
      {
        type: "frame",
        value: "wood"
      },
      {
        type: "finish",
        value: "matte"
      }
    ]
  },
  {
    attributes: [
      {
        type: "frame",
        value: "wood"
      },
      {
        type: "finish",
        value: "glossy"
      }
    ]
  }
];
Run Code Online (Sandbox Code Playgroud)

Below is my code:

function getCombinations(arr) {
  if (arr.length === 0) {
    return [[]];
  }

  let [current, ...rest] = arr;
  let combinations = getCombinations(rest);

  var result = current.values.reduce(
    (accumulator, currentValue) => [
      ...accumulator,
      ...combinations.map(c => [currentValue, ...c])
    ],
    []
  );
  console.log("result is ");
  console.log(result);
  return result;
}

let varientCombinations = getCombinations(varientSections);
console.log(varientCombinations);

let updatedVarientDetails = [];
varientSections.forEach((varientSection, index) => {
  let type = varientSection.type;
  varientCombinations.forEach(combination => {
    let obj = [
      {
        type: type,
        value: combination[index]
      },
    ];
    updatedVarientDetails.push(obj);
  });
});

console.log(updatedVarientDetails);
Run Code Online (Sandbox Code Playgroud)

Nin*_*olz 5

您可以获取笛卡尔积,并在以后提供所需的样式。名称和值取自移交的对象。

该算法采用所有键/值对,并具有对值的严格视图,这意味着如果找到了数组或对象w && typeof w === "object",则实际部分将用于添加其他键/值对。

例如,一个具有两个属性的小对象

{ a: 1, b: [2, 3] }
Run Code Online (Sandbox Code Playgroud)

产量

[
    { a: 1, b: 2 },
    { a: 1, b: 3 }
]
Run Code Online (Sandbox Code Playgroud)

更高级的对象,例如

{ a: 1, b: { c: { d: [2, 3], e: [4, 5] } } }
Run Code Online (Sandbox Code Playgroud)

产生与给定相同的结构

[
    {
        a: 1,
        b: {
            c: { d: 2, e: 4 }
        }
    },
    {
        a: 1,
        b: {
            c: { d: 2, e: 5 }
        }
    },
    {
        a: 1,
        b: {
            c: { d: 3, e: 4 }
        }
    },
    {
        a: 1,
        b: {
            c: { d: 3, e: 5 }
        }
    }
]
Run Code Online (Sandbox Code Playgroud)

这意味着从任何找到的子对象中获取笛卡尔乘积并将其与实际值组合。

{ a: 1, b: [2, 3] }
Run Code Online (Sandbox Code Playgroud)
[
    { a: 1, b: 2 },
    { a: 1, b: 3 }
]
Run Code Online (Sandbox Code Playgroud)