gpt4 book ai didi

Javascript 按键计算对象数组的百分比总计

转载 作者:行者123 更新时间:2023-12-01 03:27:58 24 4
gpt4 key购买 nike

假设我在 Javascript 中有以下数据结构:

 const data = [
{year: 2017, kind: "mammal", animal: "dog", total: 5},
{year: 2017, kind: "mammal", animal: "dog", total: 3},
{year: 2016, kind: "mammal", animal: "dog", total: 5},
{year: 2016, kind: "bird", animal: "chicken", total: 5},
{year: 2016, kind: "bird", animal: "chicken", total: 90}
];

我希望能够指定一个或多个键(例如year)来对数据进行分组。因此,如果我只是将 year 指定为分组依据,我希望计算总数如下:

 [{year: 2017, kind: "mammal", animal: "dog", count: 8, pct: 1},
{year: 2016, kind: "mammal", animal: "dog", count: 5, pct: 0.05},
{year: 2016, kind: "bird", animal: "chicken", count: 95, pct: 0.95}]

或者,如果我按年份种类指定组总数,我需要以下内容:

 [{year: 2017, kind: "mammal", animal: "dog", count: 8, pct: 1},
{year: 2016, kind: "mammal", animal: "dog", count: 5, pct: 1},
{year: 2016, kind: "bird", animal: "chicken", count: 95, pct: 1}]

我如何在 Javascript 中实现这个?我已经考虑过使用 d3 的嵌套函数,但到目前为止还没有成功地理解实现。

最佳答案

我认为在尝试实现它之前,你需要清楚地弄清楚你的需求。根据您的描述,我的理解是,对于结果,年份+种类+动物是结果项的关键。并且您指定的组仅用于计算百分比。

首先根据年份+种类+动物对项目进行分组

function groupItems(data){
var resultMap = {};
for(var index in data){
var item = data[index];
var key = item['year']+'###'+item['kind']+'###'+item['animal'];
if(!resultMap[key]){
//copy the item to a new object to make sure it does not update the origin data
resultMap[key] = Object.assign({}, item);
}else{
// sum them up
resultMap[key]['total'] = resultMap[key]['total'] +item['total'];
}
}
return Object.values(resultMap);
}

现在我们有了项目,您需要的是根据指定的组计算百分比。

function computePercentage(groupedItems, withGroups){
//compute the total items per specified group
var totalItemsPerGroup = {};
for(var index in groupedItems){
var item = groupedItems[index];
var keyValues = getValuesFromObjectWithKeys(item, withGroups);
var strKey = keyValues.join('###');
if(totalItemsPerGroup[strKey] != undefined){
totalItemsPerGroup[strKey] += item['total'];
}else{
totalItemsPerGroup[strKey] = item['total'];
}
}
// compute percentage based on the total items per specified group calculated
var result = [];
for(var index in groupedItems){
var item = groupedItems[index];
var keyValues = getValuesFromObjectWithKeys(item, withGroups);
var strKey = keyValues.join('###');
var totalCount = totalItemsPerGroup[strKey];
var percentage = totalCount == 0 ? 0 : item['total'] / totalCount;
var resultItem = {};
resultItem['year'] = item['year'];
resultItem['kind'] = item['kind'];
resultItem['animal'] = item['animal'];
resultItem['count'] = item['total'];
resultItem['pct'] = percentage;
result.push(resultItem);
}
return result;
}

// a helper function to get values based on specified keys
function getValuesFromObjectWithKeys(obj, keys){
var result = [];
for(var i in keys){
if(obj[keys[i]]){
result.push(obj[keys[i]]);
}
}
return result;
}

因此要使用这些功能:

var grouppedItems = groupItems(data);
var yearPercentage = computePercentage(grouppedItems , ['year']);
console.log(JSON.stringify(yearPercentage));
var yearKindPercentage = computePercentage(grouppedItems , ['year', 'kind']);
console.log(JSON.stringify(yearKindPercentage ));
var kindAnimalPercentage = computePercentage(grouppedItems , ['kind', 'animal']);
console.log(JSON.stringify(kindAnimalPercentage ));

关于Javascript 按键计算对象数组的百分比总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44706451/

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