admin管理员组文章数量:1317915
I have an array of object some with same month name but different values based on day. How do we group array object based on same property value for example:
[ { month: 'Jan', value: 3 },
{ month: 'Jan', value: 3.5 },
{ month: 'Feb', value: 2.1 },
{ month: 'Mar', value: 6 },
{ month: 'Apr', value: 4.3 },
{ month: 'May', value: 5.5 },
{ month: 'Jun', value: 7 },
{ month: 'Jun', value: 9 },
{ month: 'Jul', value: 7 },
{ month: 'Jul', value: 9 },
{ month: 'Jul', value: 7 },
{ month: 'Aug', value: 9 },
{ month: 'Sep', value: 9 },
{ month: 'Sep', value: 9 },
{ month: 'Oct', value: 8 },
{ month: 'Oct', value: 5 },
{ month: 'Oct', value: 3 },
{ month: 'Nov', value: 12 },
{ month: 'Nov', value: 19.5 } ]
So what i want to get is new array with month and sum of month values:
[{month:"Jan", val:6.5},{month:"Feb", val:5.6},{month:"Mar", val:6},].
I have an array of object some with same month name but different values based on day. How do we group array object based on same property value for example:
[ { month: 'Jan', value: 3 },
{ month: 'Jan', value: 3.5 },
{ month: 'Feb', value: 2.1 },
{ month: 'Mar', value: 6 },
{ month: 'Apr', value: 4.3 },
{ month: 'May', value: 5.5 },
{ month: 'Jun', value: 7 },
{ month: 'Jun', value: 9 },
{ month: 'Jul', value: 7 },
{ month: 'Jul', value: 9 },
{ month: 'Jul', value: 7 },
{ month: 'Aug', value: 9 },
{ month: 'Sep', value: 9 },
{ month: 'Sep', value: 9 },
{ month: 'Oct', value: 8 },
{ month: 'Oct', value: 5 },
{ month: 'Oct', value: 3 },
{ month: 'Nov', value: 12 },
{ month: 'Nov', value: 19.5 } ]
So what i want to get is new array with month and sum of month values:
[{month:"Jan", val:6.5},{month:"Feb", val:5.6},{month:"Mar", val:6},].
Share
Improve this question
asked Nov 23, 2018 at 9:58
EndarEndar
733 silver badges10 bronze badges
2
- The array always has this arrange (from Jan to Nov)? – Mohammad Commented Nov 23, 2018 at 10:01
- 2 Possible duplicate of Most efficient method to groupby on a array of objects – H77 Commented Nov 23, 2018 at 10:03
3 Answers
Reset to default 6You could take a Map
and reduce the array by adding the value to the month nad get new arrays from the grouped result.
var data = [{ month: 'Jan', value: 3 }, { month: 'Jan', value: 3.5 }, { month: 'Feb', value: 2.1 }, { month: 'Mar', value: 6 }, { month: 'Apr', value: 4.3 }, { month: 'May', value: 5.5 }, { month: 'Jun', value: 7 }, { month: 'Jun', value: 9 }, { month: 'Jul', value: 7 }, { month: 'Jul', value: 9 }, { month: 'Jul', value: 7 }, { month: 'Aug', value: 9 }, { month: 'Sep', value: 9 }, { month: 'Sep', value: 9 }, { month: 'Oct', value: 8 }, { month: 'Oct', value: 5 }, { month: 'Oct', value: 3 }, { month: 'Nov', value: 12 }, { month: 'Nov', value: 19.5 }],
result = Array.from(
data.reduce(
(m, { month, value }) => m.set(month, (m.get(month) || 0) + value),
new Map
),
([month, value]) => ({ month, value })
);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
A simple human-readable solution can be as follows:
var input = [ { month: 'Jan', value: 3 },
{ month: 'Jan', value: 3.5 },
{ month: 'Feb', value: 2.1 },
{ month: 'Mar', value: 6 },
{ month: 'Apr', value: 4.3 },
{ month: 'May', value: 5.5 },
{ month: 'Jun', value: 7 },
{ month: 'Jun', value: 9 },
{ month: 'Jul', value: 7 },
{ month: 'Jul', value: 9 },
{ month: 'Jul', value: 7 },
{ month: 'Aug', value: 9 },
{ month: 'Sep', value: 9 },
{ month: 'Sep', value: 9 },
{ month: 'Oct', value: 8 },
{ month: 'Oct', value: 5 },
{ month: 'Oct', value: 3 },
{ month: 'Nov', value: 12 },
{ month: 'Nov', value: 19.5 } ];
var result = [];
for (var i = 0; i < input.length; i++) {
var data = input[i];
var found=false;
for(var j=0; j<result.length; j++) {
if(result[j].month === data.month) {
found=true;
result[j].value += data.value;
break;
}
}
if(!found) {
result.push(data);
}
}
//The result array is your desired result
console.log(result);
result = input.reduce(function(res, value){
if (!res[value.month]) {
res[value.month] = {
month: value.month,
value: 0
}
result.push(res[value.month])
}
res[value.month].value += value.value
return res;
}, {});
console.log(result)
本文标签: Group javascript array object by same keyStack Overflow
版权声明:本文标题:Group javascript array object by same key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742023612a2415136.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论