You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS中按start_date匹配聚合多父键子属性值的无循环优雅方案问询

问题描述

现有如下数据结构:

{
    key1: [
        {
            a: 123,
            b: 100,
            c: 300,
            start_date: "05-01-2021"
        },
        {
            a: 123,
            b: 100,
            c: 300,
            start_date: "06-01-2021"
        },
        {
            a: 123,
            b: 100,
            c: 300,
            start_date: "07-01-2021"
        }
    ],
    key2: [
        {
            a: 123,
            b: 100,
            start_date: "05-01-2021"
        },
        {
            a: 123,
            b: 100,
            start_date: "06-01-2021"
        },
        {
            a: 123,
            b: 100,
            start_date: "07-01-2021"
        }
    ]
}

核心需求:

  • 所有数组长度一致,每个子对象的start_date字段作为匹配依据
  • 将任意数量父键(key1到keyN)下,相同start_date的子对象属性聚合求和,新增key3字段存储结果(不存在的属性保留现有值,比如key2无c字段则取key1的c值)
  • 需实现无需手动编写for循环的优雅方案,预期结果如下:
{
    key1: [
        {
            a: 123,
            b: 100,
            c: 300,
            start_date: "05-01-2021"
        },
        {
            a: 123,
            b: 100,
            c: 300,
            start_date: "06-01-2021"
        },
        {
            a: 123,
            b: 100,
            c: 300,
            start_date: "07-01-2021"
        }
    ],
    key2: [
        {
            a: 123,
            b: 100,
            start_date: "05-01-2021"
        },
        {
            a: 123,
            b: 100,
            start_date: "06-01-2021"
        },
        {
            a: 123,
            b: 100,
            start_date: "07-01-2021"
        }
    ],
    key3: [
        {
            a: 246,
            b: 200,
            c: 300,
            start_date: "05-01-2021"
        },
        {
            a: 246,
            b: 200,
            c: 300,
            start_date: "06-01-2021"
        },
        {
            a: 246,
            b: 200,
            c: 300,
            start_date: "07-01-2021"
        }
    ]
}
优雅实现方案

利用Object.values、Array.map和Array.reduce组合实现,完全避免手动for循环:

const data = {
    key1: [
        { a: 123, b: 100, c: 300, start_date: "05-01-2021" },
        { a: 123, b: 100, c: 300, start_date: "06-01-2021" },
        { a: 123, b: 100, c: 300, start_date: "07-01-2021" }
    ],
    key2: [
        { a: 123, b: 100, start_date: "05-01-2021" },
        { a: 123, b: 100, start_date: "06-01-2021" },
        { a: 123, b: 100, start_date: "07-01-2021" }
    ]
};

// 提取所有待聚合的数组
const arrays = Object.values(data);

// 生成聚合后的key3数组
const key3 = arrays[0].map((_, index) => {
    // 获取同一索引下所有数组的子对象
    const items = arrays.map(arr => arr[index]);
    // 聚合属性值
    return items.reduce((acc, item) => {
        Object.keys(item).forEach(key => {
            if (key === 'start_date') {
                acc[key] = item[key];
            } else {
                acc[key] = (acc[key] || 0) + item[key];
            }
        });
        return acc;
    }, {});
});

// 合并到原数据
const result = { ...data, key3 };

console.log(result);

代码说明

  1. Object.values(data):提取原数据中所有的数组(key1、key2对应的数组)
  2. arrays[0].map(...):以第一个数组的长度为基准(题目说明所有数组长度一致),遍历每个索引位置
  3. arrays.map(arr => arr[index]):获取同一索引下所有数组的子对象,确保对应同一start_date
  4. items.reduce(...):对同一日期的所有对象属性聚合,start_date直接保留,其他属性累加求和,不存在的属性默认取0再加当前值
  5. { ...data, key3 }:将聚合后的数组作为key3合并到原数据中

内容的提问来源于stack exchange,提问作者hackerman90219

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 04:45:31