简体   繁体   English

如何对数组内对象中具有相同键的所有值求和并求平均值

[英]how to sum up all values with same keys in an object inside an array and get their average

so i have this json and i need to add the values of each the same keys that the object has and will also compute their average, here is my json 所以我有这个json,我需要添加对象具有的每个相同键的值,并且还要计算它们的平均值,这是我的json

var arraySeries = [{"country":"United States","data":2},
                   {"country":"Venezuela","data":0},
                   {"country":"Singapore","data":3},
                   {"country":"United States","data":0},
                   {"country":"Germany","data":2},
                   {"country":"United States","data":2},
                   {"country":"Canada","data":2},
                   {"country":"Germany","data":-4}];

and here is my expected result 这是我的预期结果

var newArraySeries = [{"country":"United States","data":[1.33333]},
                   {"country":"Venezuela","data":[0]},
                   {"country":"Singapore","data":[3]},
                   {"country":"Canada","data":[2]},
                   {"country":"Germany","data":[-1]}];

the data in newArraySeries should be in array, I'm going to use it in my highchart map.. i did some google research but i can't find any article like this.I hope you can help me newArraySeries中的数据应该在数组中,我将在highchart映射中使用它。.我做了一些google研究,但找不到任何这样的文章。希望您能对我有所帮助

Solved with standard javascript and using temp variables 用标准javascript解决并使用临时变量

var countries = {}
var countries_count = {}
var newArraySeries = []
arraySeries.forEach(
    function(e){
        if(!countries[e.country]){
            countries[e.country] = 0
            countries_count[e.country] = 0
        }
        countries[e.country] += e.data
        countries_count[e.country]++
    }
)
for(var country in countries){
    newArraySeries.push({country : country, data : countries[country] / countries_count[country]})
}

You can do it with Alasql library. 您可以使用Alasql库来实现。

var arraySeries = [{"country":"United States","data":2},
           {"country":"Venezuela","data":0}];

var res = alasql('SELECT country, AVG(data) AS data FROM ? GROUP BY country',
                  [arraySeries]);

Try this example at jsFiddle. 在jsFiddle尝试此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 object 数组中获取特定键值的总和 - How to get sum of specific keys values from array of object 如何获取具有多个键的对象数组中所有键的值,然后将它们全部相加[暂停] - How to get values of all keys in array of objects with more than one keys and then sum of them all [on hold] 如何从对象数组的多个键中获取所有值? - How to get all the values from multiple keys of an array of object? 如何对 JavaScript 中具有相同值的数组求和? - How to sum up array with same values in JavaScript? 如何对数组条目进行分组和合并,并对多个常见(但不是全部)键的值求和? - How to group and merge array entries and to sum-up values on multiple common (but not all) keys? 如何总结对象数组中的所有项目? - How to sum up all items in an object array? 如何使用相同的键对对象值求和并返回一个对象? - How to sum object values with the same keys and return one object? 获取JavaScript对象中具有匹配键的所有值的总和? - Get a sum of all values that have matching keys in a JavaScript Object? 如何根据匹配的对象键获得平均值 [代码] - how to get average of values based on matcching object keys [code] 整理对象数组中的相同键并总结javascript的值 - collating same keys in object array and summing up the values javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM