admin管理员组文章数量:1403361
I have an array where I would like to get the unique values and the number of their occurrences, and sum the matching pany names. I'm using lodash, and so far I'm able to get each unique value.
How can i efficiently get the number of each occurrence, and sum them? Price data is strings.
var data = [
{"cat":"IT","pany":"Apple", "price":"100.5"},
{"cat":"IT","pany":"Apple", "price":"100"},
{"cat":"IT","pany":"Google", "price": "100"},
{"cat":"IT","pany":"Apple", "price": "100"}
];
Result I need:
Company | Count | Sum
Apple | 3 | 300.5
Google | 1 | 100
My code so far:
var result = _.map(_.uniqBy(data, 'pany'), function (item) {
$('table').append('<tr><td>'+itempany+'</td></tr>');
});
Is it possible to do sum and count inside the same _.map function?
I have an array where I would like to get the unique values and the number of their occurrences, and sum the matching pany names. I'm using lodash, and so far I'm able to get each unique value.
How can i efficiently get the number of each occurrence, and sum them? Price data is strings.
var data = [
{"cat":"IT","pany":"Apple", "price":"100.5"},
{"cat":"IT","pany":"Apple", "price":"100"},
{"cat":"IT","pany":"Google", "price": "100"},
{"cat":"IT","pany":"Apple", "price": "100"}
];
Result I need:
Company | Count | Sum
Apple | 3 | 300.5
Google | 1 | 100
My code so far:
var result = _.map(_.uniqBy(data, 'pany'), function (item) {
$('table').append('<tr><td>'+item.pany+'</td></tr>');
});
Is it possible to do sum and count inside the same _.map function?
Share Improve this question asked Feb 12, 2017 at 17:37 Filip BlaauwFilip Blaauw 7612 gold badges17 silver badges30 bronze badges2 Answers
Reset to default 5Pure JS(Ecmascript5) solution using Array.prototype.reduce()
and Object.keys()
functions:
var data = [
{"cat":"IT","pany":"Apple", "price":"100.5"},
{"cat":"IT","pany":"Apple", "price":"100"},
{"cat":"IT","pany":"Google", "price": "100"},
{"cat":"IT","pany":"Apple", "price": "100"}
], rows = "";
var result = data.reduce(function(r, o) {
if (r[o.pany]){
++r[o.pany].count;
r[o.pany].price += Number(o.price);
} else {
r[o.pany] = {count: 1, price: Number(o.price)};
}
return r;
}, {});
Object.keys(result).forEach(function(name){
rows += '<tr><td>'+ name +'</td>' +
'<td>'+ result[name].count +'</td>' +
'<td>'+ result[name].price +'</td></tr>';
});
$('table').append(rows);
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<th>Company</th>
<th>Count</th>
<th>Sum</th>
</tr>
</table>
You can first groupBy
pany and then map
and reduce
to return array of objects for each pany.
var data = [
{"cat":"IT","pany":"Apple", "price":"100.5"},
{"cat":"IT","pany":"Apple", "price":"100"},
{"cat":"IT","pany":"Google", "price": "100"},
{"cat":"IT","pany":"Apple", "price": "100"}
];
var group = _.groupBy(data, 'pany')
var result = _.map(_.keys(group), function(e) {
return _.reduce(group[e], function(r, o) {
return r.count += +o.price, r
}, {Company: e, count: 0, sum: group[e].length})
})
console.log(result)
<script src="https://cdnjs.cloudflare./ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
本文标签: javascriptGet unique valuessum and count from an array using lodashStack Overflow
版权声明:本文标题:javascript - Get unique values, sum and count from an array using lodash - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744363423a2602656.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论