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

TS/JS中按属性分组并多属性求和失败问题求助

分组求和问题:多属性求和时第二个属性值为undefined

尝试对对象数组按指定属性分组,同时对2个及以上属性求和统计,但当前实现仅能得到分组属性与第一个求和属性的结果,第二个求和属性值为undefined。

初始数组结构

combinedItems的结构如下:

[
  {
    itemSi: 1,
    productId: 'one',
    taxableValue: 100,
    taxValue: 10,
    product: { id: 'one', productName: 'product one', taxId: 'one' },
    tax: { id: 'one', taxName: 'tax one' }
  },
  {
    itemSi: 2,
    productId: 'two',
    taxableValue: 100,
    taxValue: 10,
    product: { id: 'two', productName: 'product two', taxId: 'one' },
    tax: { id: 'one', taxName: 'tax one' }
  }
]

需求

按taxName分组,同时求和taxableValue和taxValue。

当前实现代码

const summaryValues = new Map<any []>();

for(const {tax, taxableValue, taxValue} of combinedItems) {
  summaryValues.set(
      tax.taxName,
      (summaryValues.get(tax.taxName) || 0) + taxableValue,
      (summaryValues.get(tax.taxName) || 0) + taxValue,
  );
}

const summaries = [...summaryValues]
console.log(summaries);

const taxSummary = summaries.map(x => ({ 
  taxName: x[0], 
  taxableValue: x[1],
  taxValue: x[2]
}));

console.log(taxSummary)

运行结果

[ [ 'tax one', 200 ] ]

[ { taxName: 'tax one', taxableValue: 200, taxValue: undefined } ]

combinedItems生成逻辑

const items: any[] = [
    {
        itemSi: 1,
        productId: "one",
        taxableValue: 100,
        taxValue: 10
    },
    {
        itemSi: 2,
        productId: "two",
        taxableValue: 100,
        taxValue: 10
    }
];

const products: any[] = [
    {
        id: "one",
        productName:"product one",
        taxId: "one"
    },
    {
        id: "two",
        productName:"product two",
        taxId: "one"
    }
];

const taxes: any[] = [
    {
        id: "one",
        taxName:"tax one"
    },
    {
        id: "two",
        taxName:"tax two"
    }
];

let combinedItems: any [] = [] 

combinedItems = items.map(x => {
    let pdtItem = products.find(z => z.id === x.productId);
    let taxItem = taxes.find(z => z.id === pdtItem.taxId);
    let item = {...x, product: {...pdtItem }, tax: {...taxItem}};
    return item;
});

console.log(combinedItems)

问题原因

Map.set()方法仅接受两个参数:键(key)和值(value),传入的第三个参数会被直接忽略。当前代码只把累加后的taxableValue作为值存入Map,自然无法获取到taxValue的结果。

修正方案

将需要求和的两个属性封装成一个对象作为Map的值,循环时对对象中的属性分别累加:

修正后的代码

const summaryValues = new Map();

for(const { tax, taxableValue, taxValue } of combinedItems) {
  // 获取当前分组的统计对象,不存在则初始化默认值
  const current = summaryValues.get(tax.taxName) || { taxableValue: 0, taxValue: 0 };
  // 分别累加两个属性的值
  summaryValues.set(tax.taxName, {
    taxableValue: current.taxableValue + taxableValue,
    taxValue: current.taxValue + taxValue
  });
}

// 转换为目标格式数组
const taxSummary = Array.from(summaryValues, ([taxName, values]) => ({
  taxName,
  taxableValue: values.taxableValue,
  taxValue: values.taxValue
}));

console.log(taxSummary);

运行结果

[ { taxName: 'tax one', taxableValue: 200, taxValue: 20 } ]

内容的提问来源于stack exchange,提问作者Dasharath Sagar Nariman

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 02:01:08