计算javascript对象数组中重复项的平均值 [英] Calculate average of duplicates in a javascript array of objects

查看:28
本文介绍了计算javascript对象数组中重复项的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象数组:

<预><代码>[{"market": "Qacha's nek","commodity": 55,"price": "90","month": "04","year": "2017"},{"market": "Mohales Hoek","commodity": 55,"price": "75","month": "04","year": "2017"},{"market": "Mafeteng","commodity": 55,"price": "75","month": "04","year": "2017"},{"market": "Maseru","commodity": 55,"price": "69","month": "04","year": "2017"},{"market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"},{"market": "Leribe","commodity": 55,"price": "64","month": "04","year": "2017"},{"market": "Butha-Buthe","commodity": 55,"price": "65","month": "04","year": "2017"},{"market": "Thaba-Tseka","commodity": 55,"price": "82","month": "04","year": "2017"},{"market": "Thaba-Tseka","commodity": 55,"price": "81","month": "04","year": "2017"},{"market": "Maseru", "commodity": 55,"price": "74,99","month": "04","year": "2017"}]

我正在尝试按平均价格汇总重复项.

因此,识别重复行的关键是除价格之外的所有属性,必须按平均值聚合.

在上面的数据中,例如第 5 行和第 7 行:

5) "market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"7)市场":Butha-Buthe",商品":55,价格":65",月份":04",年份":2017"

是重复的,我想合并它们并取其价格的平均值.

我试图使用 reduce 函数,但我不知道如何识别重复值,尤其是在它们未排序的情况下.

我发布了代码,但它没用,因为我无法理解如何使用 reduce 识别重复项:

var data = [{"market": "Qacha's nek","commodity": 55,"price": "90","month": "04","year": "2017"},{"market": "Mohales Hoek","commodity": 55,"price": "75","month": "04","year": "2017"},{"market": "Mafeteng","commodity": 55,"price": "75","month": "04","year": "2017"},{"market": "Maseru","commodity": 55,"price": "69","month": "04","year": "2017"},{"market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"},{"market": "Leribe","commodity": 55,"price": "64","month": "04","year": "2017"},{"market": "Butha-Buthe","commodity": 55,"price": "65","month": "04","year": "2017"},{"market": "Thaba-Tseka","commodity": 55,"price": "82","month": "04","year": "2017"},{"market": "Thaba-Tseka","commodity": 55,"price": "81","month": "04","year": "2017"},{"market": "Maseru","commodity": 55,"price": "74,99","month": "04","year": "2017"}];var avg = data.reduce(function(result, current) {控制台日志(结果,当前);如果(!结果){结果=当前;}别的 {if(result.market==current.market){控制台日志(当前市场);}}});

这里有一个 jsfiddle,我试图了解 reduce 函数的工作原理:https://jsfiddle.net/brainsengineering/7tmdx0kg/7/

解决方案

您可以为所需属性使用组合键,并将 price 格式替换为数字可解析格式.

var data = [{ market: "Qacha's nek", 商品: 55, price: "90", month: "04", year: "2017" }, { 市场: "Mohales Hoek", 商品: 55, 价格: "75", 月份: "04", 年份: "2017" }, { 市场: "Mafeteng", 商品: 55, 价格:75",月份:04",年份:2017"},{市场:Maseru",商品:55,价格:69",月份:04",年份:2017"},{市场:Butha-Buthe",商品:55,价格:66",月份:04",年份:2017" }, { 市场:Leribe",商品:55,价格:64",月份:04"", year: "2017" }, { market: "Butha-Buthe", 商品: 55, price: "65", month: "04", year: "2017" }, { market: "Thaba-Tseka",商品:55,价格:82",月份:04",年份:2017"},{市场:Thaba-Tseka",商品:55,价格:81",月份:04",年份:"2017" }, { 市场: "Maseru", 商品: 55, 价格: "74,99", 月份: "04", year: "2017" }],键 = ['市场', '商品', '月', '年'],计数 = {},结果 = data.reduce(function (r, o) {var key = keys.map(function (k) { return o[k]; }).join('|');如果 (!count[key]) {count[key] = { sum: +o.price.replace(',', '.'), data: JSON.parse(JSON.stringify(o)) };计数[键].data.count = 1;r.push(count[key].data);} 别的 {count[key].sum += +o.price.replace(',', '.');count[key].data.price = (count[key].sum/++count[key].data.count).toString();}返回 r;}, []);console.log(result);

.as-console-wrapper { max-height: 100% !important;顶部:0;}

I have an array of objects:

[
{"market": "Qacha's nek","commodity": 55,"price": "90","month": "04","year": "2017"}, 
{"market": "Mohales Hoek","commodity": 55,"price": "75","month": "04","year": "2017"}, 
{"market": "Mafeteng","commodity": 55,"price": "75","month": "04","year": "2017"}, 
{"market": "Maseru","commodity": 55,"price": "69","month": "04","year": "2017"}, 
{"market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"}, 
{"market": "Leribe","commodity": 55,"price": "64","month": "04","year": "2017"}, 
{"market": "Butha-Buthe","commodity": 55,"price": "65","month": "04","year": "2017"}, 
{"market": "Thaba-Tseka","commodity": 55,"price": "82","month": "04","year": "2017"},
{"market": "Thaba-Tseka","commodity": 55,"price": "81","month": "04","year": "2017"},
{"market": "Maseru",    "commodity": 55,"price": "74,99","month": "04","year": "2017"}
]

I'm trying to aggregate duplicates by price average.

So the keys to identifying duplicated rows are all the properties except price, that must be aggregated by average.

In the data above, for example, line 5 and 7:

5) "market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"
7) "market": "Butha-Buthe","commodity": 55,"price": "65","month": "04","year": "2017"

are duplicates and I want to merge them and make the average of their price value.

I was trying to use the reduce function, but I can't figure out how to identify duplicated values, especially if they are not sorted.

I post the code, but it's useless as I can't understand how to identify duplicates with reduce:

var data = [
{"market": "Qacha's nek","commodity": 55,"price": "90","month": "04","year": "2017"}, 
{"market": "Mohales Hoek","commodity": 55,"price": "75","month": "04","year": "2017"}, 
{"market": "Mafeteng","commodity": 55,"price": "75","month": "04","year": "2017"}, 
{"market": "Maseru","commodity": 55,"price": "69","month": "04","year": "2017"}, 
{"market": "Butha-Buthe","commodity": 55,"price": "66","month": "04","year": "2017"}, 
{"market": "Leribe","commodity": 55,"price": "64","month": "04","year": "2017"}, 
{"market": "Butha-Buthe","commodity": 55,"price": "65","month": "04","year": "2017"}, 
{"market": "Thaba-Tseka","commodity": 55,"price": "82","month": "04","year": "2017"},
{"market": "Thaba-Tseka","commodity": 55,"price": "81","month": "04","year": "2017"},
{"market": "Maseru","commodity": 55,"price": "74,99","month": "04","year": "2017"}
];

var avg = data.reduce(function(result, current) {
			console.log(result,current);
      if(!result){
      	result=current;
      }
      else {
      	if(result.market==current.market){
        	console.log(current.market);
        }
      }
});

Here a jsfiddle where I was trying to understand how the reduce function works: https://jsfiddle.net/brainsengineering/7tmdx0kg/7/

解决方案

You could take a combined key for the wanted properties and replace the price format to a numerical parsable format.

var data = [{ market: "Qacha's nek", commodity: 55, price: "90", month: "04", year: "2017" }, { market: "Mohales Hoek", commodity: 55, price: "75", month: "04", year: "2017" }, { market: "Mafeteng", commodity: 55, price: "75", month: "04", year: "2017" }, { market: "Maseru", commodity: 55, price: "69", month: "04", year: "2017" }, { market: "Butha-Buthe", commodity: 55, price: "66", month: "04", year: "2017" }, { market: "Leribe", commodity: 55, price: "64", month: "04", year: "2017" }, { market: "Butha-Buthe", commodity: 55, price: "65", month: "04", year: "2017" }, { market: "Thaba-Tseka", commodity: 55, price: "82", month: "04", year: "2017" }, { market: "Thaba-Tseka", commodity: 55, price: "81", month: "04", year: "2017" }, { market: "Maseru", commodity: 55, price: "74,99", month: "04", year: "2017" }],
    keys = ['market', 'commodity', 'month', 'year'],
    count = {},
    result = data.reduce(function (r, o) {
        var key = keys.map(function (k) { return o[k]; }).join('|');
        if (!count[key]) {
            count[key] = { sum: +o.price.replace(',', '.'), data: JSON.parse(JSON.stringify(o)) };
            count[key].data.count = 1;
            r.push(count[key].data);
        } else {
            count[key].sum += +o.price.replace(',', '.');
            count[key].data.price = (count[key].sum / ++count[key].data.count).toString();
        }
        return r;
    }, []);

console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }

这篇关于计算javascript对象数组中重复项的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆