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 badges
Add a ment  | 

2 Answers 2

Reset to default 5

Pure 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