![](/img/trans.png)
[英]How to get all specific values from an array of objects with nested arrays of objects?
[英]How to get the combination of array values from nested arrays in an array of objects
我有一個具有以下結構的對象數組:
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 }
}
}
]
Thant意味着,從任何找到的子對象中獲取笛卡爾積並將其與實際值組合。
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; }
你可以簡化它:
var variantSections = [ { type: "frame", values: ["black", "white", "wood"] }, { type: "finish", values: ["matte", "glossy"] } ]; // iterate through each variantSection and create objects like {"type": "frame", "value": "black"} var sections = variantSections.map(variant => { return variant.values.map(val => ({type: variant.type, value: val})) }); // then iterate through the two resulting arrays of objects, combining each into the attributes object you want var results = []; for (var i = 0; i < sections[0].length; i++) { for (var j = 0; j < sections[1].length; j++) { results.push({attributes: [sections[0][i], sections[1][j]]}); } } console.log(JSON.parse(JSON.stringify(results)));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.