gpt4 book ai didi

javascript - 如何从对象数组中的嵌套数组中获取数组值的组合

转载 作者:搜寻专家 更新时间:2023-10-30 22:19:32 25 4
gpt4 key购买 nike

我有一个具有以下结构的对象数组:

 var varientSections = [
{
type: "frame",
values: ["black", "white", "wood"]
},
{
type: "finish",
values: ["matte", "glossy"]
}
];

我想获取数组值的组合并用它创建一个新列表。 现在我可以使用名为getCombination(varientSections) 的方法从嵌套数组值中检索组合。但是,我不知道如何创建具有以下结构的新列表:

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"
}
]
}
];

下面是我的代码:

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);

最佳答案

您可以获得笛卡尔积,稍后再赋予它所需的样式。名称和值取自移交的对象。

该算法采用所有键/值对并对值有严格的看法,这意味着如果找到数组或对象,因此 w && typeof w === "object",实际部分用于添加额外的键/值对。

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

{ a: 1, b: [2, 3] }

产量

[
{ a: 1, b: 2 },
{ a: 1, b: 3 }
]

更高级一点的对象,比如

{ a: 1, b: { c: { d: [2, 3], e: [4, 5] } } }

产生与给定相同的结构

[
{
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 }
}
}
]

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

const
getCartesian = object => Object.entries(object).reduce(
(r, [key, value]) => {
let temp = [];
r.forEach(s =>
(Array.isArray(value) ? value : [value]).forEach(w =>
(w && typeof w === "object" ? getCartesian(w) : [w]).forEach(x =>
temp.push({ ...s, [key]: x })
)
)
);
return temp;
},
[{}]
),
data = [{ type: "frame", value: ["black", "white", "wood"] }, { type: "finish", value: ["matte", "glossy"] }],
result = getCartesian(data)
.map(o => ({ attributes: Object.assign([], o).map(({ ...o }) => o) }));

console.log(result);

console.log(getCartesian({ a: 1, b: { c: { d: [2, 3], e: [4, 5] } } }));
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何从对象数组中的嵌套数组中获取数组值的组合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56972012/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com