最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get distinct values and sum the total on JSON using JS - Stack Overflow

programmeradmin1浏览0评论

How do you get the data from distinct values also sum the total values from the same unique row

Here is the JSON Data result

As you can see they have the same full name. How do you get the distinct value while sum the total priceGross.

The result should be returning eg. "Khalem Williams" and adding the total priceGross " 1200 + 1200 " = 2400

result return

FullName : "Khalem Williams" 
TotalPriceGross: "2400"

I'm getting the distinct values with the code below but I'm wondering how to sum the total priceGross while also getting the distinct values.

var countTotal = [];

$.each(data.aaData.data,function(index, value){
          if ($.inArray(value.invoiceToFullName, countTotal)==-1) {
                    countTotal.push(value.invoiceToFullName);
          }
});

How do you get the data from distinct values also sum the total values from the same unique row

Here is the JSON Data result

As you can see they have the same full name. How do you get the distinct value while sum the total priceGross.

The result should be returning eg. "Khalem Williams" and adding the total priceGross " 1200 + 1200 " = 2400

result return

FullName : "Khalem Williams" 
TotalPriceGross: "2400"

I'm getting the distinct values with the code below but I'm wondering how to sum the total priceGross while also getting the distinct values.

var countTotal = [];

$.each(data.aaData.data,function(index, value){
          if ($.inArray(value.invoiceToFullName, countTotal)==-1) {
                    countTotal.push(value.invoiceToFullName);
          }
});
Share Improve this question edited Mar 4, 2017 at 2:09 gyre 16.8k4 gold badges40 silver badges47 bronze badges asked Mar 4, 2017 at 0:47 Jon MateoJon Mateo 3846 silver badges18 bronze badges 2
  • Can you confirm this is what you're asking, please: the value at data.aaData.data is an array of invoice objects, each of which contains, among others, properties invoiceToFullName and priceGross (you highlight priceNett but specified priceGross), and you'd like to pivot that into an object containing names and the sum of price? – Fissure King Commented Mar 4, 2017 at 1:31
  • {"Khalem Williams":2400, ...} output would be better and let Object merge the unique – dandavis Commented Mar 4, 2017 at 2:54
Add a ment  | 

2 Answers 2

Reset to default 7

I would honestly suggest converting your data to an object with names as keys (unique) and total gross prices as values:

{
  "Khalem Williams": 2400,
  "John Doe": 2100
}

However, I have included in the snippet below example code for how to convert your data to an array of objects as well. I used a bination of Object.keys, Array#reduce and Array#map.

var data = {
  aaData: {
    data: [{
        invoiceToFullName: "Khalem Williams",
        priceGross: 1200
      },
      {
        invoiceToFullName: "Khalem Williams",
        priceGross: 1200
      },
      {
        invoiceToFullName: "John Doe",
        priceGross: 500
      },
      {
        invoiceToFullName: "John Doe",
        priceGross: 1600
      },
    ]
  }
}

var map = data.aaData.data.reduce(function(map, invoice) {
  var name = invoice.invoiceToFullName
  var price = +invoice.priceGross
  map[name] = (map[name] || 0) + price
  return map
}, {})

console.log(map)

var array = Object.keys(map).map(function(name) {
  return {
    fullName: name,
    totalPriceGross: map[name]
  }
})

console.log(array)

Just created my own dummy data resembling yours hope you get the idea, if not you can paste your json and i can adjust my solution, was just lazy to recreate your json.

Your problem seems to be a typical group by field from sql.

Sample data:

   var resultsData = [{name: 'sipho', amount: 10}, {name: 'themba', amount: 30}, {name: 'sipho', amount: 60}, {name: 'naidoo', amount: 30}, {name: 'gupta', amount: 70}, {name: 'naidoo', amount: 10}];

Get value of customer(field) you going to group by:

var customers = $.unique(resultsData.map(function(value){return value.name}));

Go through each customer and add to their sum:

var desiredSummary = customers.map(function(customer){
    var sum =0; 
    $.each(test,function(index, value){sum+=(value.name==customer?value.amount:0)}); 
    return {customer: customer, sum: sum}
});

Hope it helps, Happy coding

发布评论

评论列表(0)

  1. 暂无评论