gpt4 book ai didi

javascript - 如何使用 ES6 过滤器和映射将平面结构转换为嵌套结构

转载 作者:行者123 更新时间:2023-12-04 10:54:56 26 4
gpt4 key购买 nike

我尝试过尝试将平面数据结构转换为嵌套结构的 ES6 过滤器和映射函数,我设法获得了所需的结果,但该解决方案感觉非常难看。如何改进它以避免使用多个 forEach 循环?

嵌套结构(我正在寻找的结果)

[
{
instance: "EU1",
testResults:
[
{
label: "PDP",
success: 10,
total: 15,
locales:
[
{
locale: "sv_SE",
success: 4,
total: 5
},
{
locale: "da_DK",
success: 3,
total: 5
},
{
locale: "no_NO",
success: 3,
total: 5
}
]
},
{
label: "PLP",
success: 12,
total: 15,
locales:
[
{
locale: "sv_SE",
success: 4,
total: 5
},
{
locale: "da_DK",
success: 4,
total: 5
},
{
locale: "no_NO",
success: 4,
total: 5
}
]
}
]
},
{
instance: "EU2",
testResults:
[
{
label: "PDP",
success: 12,
total: 15,
locales:
[
{
locale: "nl_NL",
success: 4,
total: 5
},
{
locale: "de_DE",
success: 4,
total: 5
},
{
locale: "de_AT",
success: 4,
total: 5
}
]
},
{
label: "PLP",
success: 14,
total: 15,
locales:
[
{
locale: "nl_NL",
success: 5,
total: 5
},
{
locale: "de_DE",
success: 5,
total: 5
},
{
locale: "de_AT",
success: 4,
total: 5
}
]
}
]
}
]

到目前为止,这是我所得到的,但我无法理解如何以更好的方式做到这一点,而不是多次迭代所有内容。

// Flat data structure
const flatData = [
{
instance: 'EU1',
label: 'PDP',
locale: 'sv_SE',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU1',
label: 'PDP',
locale: 'da_DK',
success: 3,
failed: 2,
skipped: 0
},
{
instance: 'EU1',
label: 'PDP',
locale: 'no_NO',
success: 3,
failed: 2,
skipped: 0
},
{
instance: 'EU1',
label: 'PLP',
locale: 'sv_SE',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU1',
label: 'PLP',
locale: 'da_DK',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU1',
label: 'PLP',
locale: 'no_NO',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU2',
label: 'PDP',
locale: 'nl_NL',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU2',
label: 'PDP',
locale: 'de_DE',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU2',
label: 'PDP',
locale: 'de_AT',
success: 4,
failed: 1,
skipped: 0
},
{
instance: 'EU2',
label: 'PLP',
locale: 'nl_NL',
success: 5,
failed: 0,
skipped: 0
},
{
instance: 'EU2',
label: 'PLP',
locale: 'de_DE',
success: 5,
failed: 0,
skipped: 0
},
{
instance: 'EU2',
label: 'PLP',
locale: 'de_AT',
success: 4,
failed: 1,
skipped: 0
}
];

function convertData(data) {
let results = [];
if (data && data.length) {
const labelNames = [...new Set(data.map(item => item.label))];
const instanceNames = [...new Set(data.map(item => item.instance))];

let instances = [];
instanceNames.forEach(instanceName => {
instances.push({
instance: instanceName,
labels: data.filter(({instance}) => instance === instanceName)
.map(item => ({
label: item.label,
locale: item.locale,
success: item.success,
total: item.success + item.failed + item.skipped
}))
});
});

let labels = [];
instances.forEach(instance => {
const instanceName = instance.instance;
labelNames.forEach(labelName => {
labels.push({
instance: instanceName,
label: labelName,
locales: getByLabel(instance, labelName),
success: getSum(instance, labelName, 'success'),
total: getSum(instance, labelName, 'total')
});
});
results.push({
instance: instanceName,
labels: labels.filter((labelObj) => labelObj.instance === instanceName)
.map(item => ({
label: item.label,
locales: item.locales,
success: item.success,
total: item.total
}))
});
});
}
return results;
}

function getByLabel(instance, value) {
let results = [];
results.push(
instance.labels.filter(({label}) => label === value)
.map(item => ({
locale: item.locale,
success: item.success,
total: item.total
}))
);
return results;
}

function getSum(instance, value, key) {
let total = 0;
instance.labels.forEach(item => {
if (item.label === value) {
total += item[key];
}
});
return total;
}

console.log(convertData(flatData));

最佳答案

您可以采取一种不进行过滤的方法,但要尊重外部和内部群体的级别。

const
data = [{ instance: 'EU1', label: 'PDP', locale: 'sv_SE', success: 4, failed: 1, skipped: 0 }, { instance: 'EU1', label: 'PDP', locale: 'da_DK', success: 3, failed: 2, skipped: 0 }, { instance: 'EU1', label: 'PDP', locale: 'no_NO', success: 3, failed: 2, skipped: 0 }, { instance: 'EU1', label: 'PLP', locale: 'sv_SE', success: 4, failed: 1, skipped: 0 }, { instance: 'EU1', label: 'PLP', locale: 'da_DK', success: 4, failed: 1, skipped: 0 }, { instance: 'EU1', label: 'PLP', locale: 'no_NO', success: 4, failed: 1, skipped: 0 }, { instance: 'EU2', label: 'PDP', locale: 'nl_NL', success: 4, failed: 1, skipped: 0 }, { instance: 'EU2', label: 'PDP', locale: 'de_DE', success: 4, failed: 1, skipped: 0 }, { instance: 'EU2', label: 'PDP', locale: 'de_AT', success: 4, failed: 1, skipped: 0 }, { instance: 'EU2', label: 'PLP', locale: 'nl_NL', success: 5, failed: 0, skipped: 0 }, { instance: 'EU2', label: 'PLP', locale: 'de_DE', success: 5, failed: 0, skipped: 0 }, { instance: 'EU2', label: 'PLP', locale: 'de_AT', success: 4, failed: 1, skipped: 0 }],
result = data.reduce((r, { instance, label, locale, success: passed, failed, skipped }) => {
var outer = r.find(q => q.instance === instance);
if (!outer) r.push(outer = { instance, testResults: [] });
var inner = outer.testResults.find(q => q.label === label);
if (!inner) outer.testResults.push(inner = { label, passed: 0, total: 0, locales: [] });
inner.locales.push({ locale, passed, total: passed + failed + skipped });
inner.passed += passed;
inner.total += passed + failed + skipped;
return r;
}, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 如何使用 ES6 过滤器和映射将平面结构转换为嵌套结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59264660/

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