gpt4 book ai didi

javascript - 如何获取javascript对象数组中的值的总计

转载 作者:行者123 更新时间:2023-11-29 16:34:42 24 4
gpt4 key购买 nike

我有以下数组:

[
{genderAge: "Male 25-39", count: 13029, weightedCount: 18475.6824262314, matchingSegment: 2},
{genderAge: "Female 55+", count: 35639, weightedCount: 32294.5926147014, matchingSegment: 8},
{genderAge: "Female 25-39", count: 23285, weightedCount: 20645.0599977815, matchingSegment: 6},
{genderAge: "Female 18-24", count: 7745, weightedCount: 8497.30029399032, matchingSegment: 5},
{genderAge: "Male 55+", count: 38018, weightedCount: 28589.2793936886, matchingSegment: 4},
{genderAge: "Male 18-24", count: 4038, weightedCount: 8122.65161312996, matchingSegment: 1},
{genderAge: "Female 40-54", count: 23051, weightedCount: 22834.3167597392, matchingSegment: 7},
{genderAge: "Male 40-54", count: 17278, weightedCount: 19681.8852663563, matchingSegment: 3}
]

我想将这个对象数组转换为一个包含两个对象的数组,其中包含男性和女性的汇总数据(计数、加权计数和匹配段)。是否有使用 Lodash 和/或 ES6 实现此目的的快捷方法?

更新:抱歉我提供了错误的数据。

最佳答案

function sumFor(pattern, data) {
return data
.filter(item => item.text.match(pattern))
.reduce((accu, item) => accu + item.total, 0);
}
sumFor(/^Male/, data); // 125480
sumFor(/^Female/, data); // 154916

关于javascript - 如何获取javascript对象数组中的值的总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52554302/

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