
本文详细阐述了如何运用 javascript 的 `reduce` 方法对复杂对象数组进行深度转换与聚合。教程通过一个具体示例,展示了如何逐层构建嵌套结构,并根据 `medico`、`rateio` 和 `convenio` 等键对数据进行分组及 `subtotal` 求和,以实现高效且结构化的数据重塑。
在深入探讨复杂数据转换之前,我们首先回顾一下 Array.prototype.reduce() 方法。reduce() 方法对数组中的每个元素执行一个由您提供的 reducer 函数,将其结果汇总为单个返回值。
reducer 函数接收四个参数:
reduce() 方法通常用于将数组扁平化、计算总和、构建新对象等场景,其灵活性使其成为处理复杂数据转换的强大工具。
假设我们有一个扁平化的对象数组,其中包含 medico(医生)、rateio(费率)、convenio(协议)和 subtotal(小计)等信息:
立即学习“Java免费学习笔记(深入)”;
const arr = [
{ medico: "med1", rateio: "rat1", convenio: "conv1", subtotal: 10 },
{ medico: "med2", rateio: "rat2", convenio: "conv2", subtotal: 10 },
{ medico: "med2", rateio: "rat2", convenio: "conv2", subtotal: 20 },
{ medico: "med1", rateio: "rat1", convenio: "conv3", subtotal: 20 },
{ medico: "med1", rateio: "rat1", convenio: "conv3", subtotal: 25 },
{ medico: "med2", rateio: "rat3", convenio: "conv4", subtotal: 15 },
{ medico: 'med2', rateio: 'rat4', convenio: 'conv3', subtotal: 10 },
];我们的目标是将这个扁平数组转换为一个高度结构化的嵌套数组,具体要求如下:
期望的输出结构如下:
const result = [
{medico: "med1", grantotals:[
{
rateio: "rat1",
grandtotals: [
{ convenio: "conv1", sum_subtotal: 10 },
{ convenio: "conv3", sum_subtotal: 45 } // 20 + 25
]
}
]},
{medico: "med2", grantotals:[
{
rateio: "rat2",
grandtotals: [
{ convenio: "conv2", sum_subtotal: 30 }, // 10 + 20
]
},
{
rateio: "rat3",
grandtotals: [
{ convenio: "conv4", sum_subtotal: 15 },
]
},
{
rateio: "rat4",
grandtotals: [
{ convenio: "conv3", sum_subtotal: 10 },
]
}
]}
];为了实现上述复杂转换,我们将利用 reduce 方法的累加器来逐步构建目标结构。核心思路是:对于原始数组中的每一个元素,我们都尝试在累加器中查找其对应的 medico、rateio 和 convenio 层级。如果存在,则更新 subtotal;如果不存在,则创建新的层级对象并添加到相应的位置。
const arr = [
{ medico: "med1", rateio: "rat1", convenio: "conv1", subtotal: 10 },
{ medico: "med2", rateio: "rat2", convenio: "conv2", subtotal: 10 },
{ medico: "med2", rateio: "rat2", convenio: "conv2", subtotal: 20 },
{ medico: "med1", rateio: "rat1", convenio: "conv3", subtotal: 20 },
{ medico: "med1", rateio: "rat1", convenio: "conv3", subtotal: 25 },
{ medico: "med2", rateio: "rat3", convenio: "conv4", subtotal: 15 },
{ medico: "med2", rateio: "rat4", convenio: "conv3", subtotal: 10 },
];
const result = arr.reduce((acc, obj) => {
// 1. 查找或创建 medico 层级
let existingMedico = acc.find((item) => item.medico === obj.medico);
if (!existingMedico) {
existingMedico = {
medico: obj.medico,
grantotals: [],
};
acc.push(existingMedico);
}
// 2. 在 medico 层级下查找或创建 rateio 层级
let existingRateio = existingMedico.grantotals.find(
(item) => item.rateio === obj.rateio
);
if (!existingRateio) {
existingRateio = {
rateio: obj.rateio,
grandtotals: [],
};
existingMedico.grantotals.push(existingRateio);
}
// 3. 在 rateio 层级下查找或创建 convenio 层级并更新 subtotal
let existingConvenio = existingRateio.grandtotals.find(
(item) => item.convenio === obj.convenio
);
if (existingConvenio) {
existingConvenio.sum_subtotal += obj.subtotal;
} else {
existingRateio.grandtotals.push({
convenio: obj.convenio,
sum_subtotal: obj.subtotal,
});
}
return acc;
}, []);
console.log(JSON.stringify(result, null, 2));arr.reduce((acc, obj) => { ... }, []);
查找或创建 medico 层级
let existingMedico = acc.find((item) => item.medico === obj.medico);
if (!existingMedico) {
existingMedico = { medico: obj.medico, grantotals: [] };
acc.push(existingMedico);
}在 medico 层级下查找或创建 rateio 层级
let existingRateio = existingMedico.grantotals.find(
(item) => item.rateio === obj.rateio
);
if (!existingRateio) {
existingRateio = { rateio: obj.rateio, grandtotals: [] };
existingMedico.grantotals.push(existingRateio);
}在 rateio 层级下查找或创建 convenio 层级并更新 subtotal
let existingConvenio = existingRateio.grandtotals.find(
(item) => item.convenio === obj.convenio
);
if (existingConvenio) {
existingConvenio.sum_subtotal += obj.subtotal;
} else {
existingRateio.grandtotals.push({
convenio: obj.convenio,
sum_subtotal: obj.subtotal,
});
}return acc;
上述 reduce 实现虽然功能正确且易于理解,但在处理大型数据集时可能会遇到性能瓶颈。主要原因是使用了 Array.prototype.find() 方法进行层层查找。find() 方法的时间复杂度为 O(N),在最坏情况下,每次迭代都需要遍历数组。如果 medico、rateio、convenio 的种类繁多,整个转换过程的复杂度将显著增加。
为了优化性能,我们可以考虑使用 JavaScript 的 Map 对象作为中间缓存,将查找操作的时间复杂度从 O(N) 降低到 O(1)。
基于 Map 的优化思路:
这种 Map 优化的方法可以显著提升处理大量数据的效率,尤其是在唯一键值较多的情况下。
Array.prototype.reduce() 是 JavaScript 中一个极其强大的数组方法,能够实现从简单的聚合到复杂的数据结构转换。通过本教程,我们学习了如何利用 reduce 方法,结合条件判断和层级查找,将一个扁平化的对象数组转换为多层级嵌套并带有聚合计算的复杂结构。
在实际应用中,理解 reduce 的工作原理及其在不同场景下的适用性至关重要。同时,对于大型数据集,我们也应关注性能问题,并考虑使用 Map 等数据结构进行优化,以确保代码的效率和可扩展性。掌握这些技巧,将使您在处理 JavaScript 数据转换任务时更加得心应手。
以上就是JavaScript reduce 高级用法:多层级数据结构转换与汇总的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号