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

javascript - Sum of an array with a condition - Stack Overflow

programmeradmin0浏览0评论

I want to sum the element value of the array, which have the same year.

var newData = [];
var data = new Array(['2013', 0], ['2013', 0],['2016', 15], ['2017', 2], ['2015', 1], ['2013', 0], ['2016', 12], ['2014', 0], ['2017', 3], ['2015', 1], ['2013', 21], ['2013', 2]);

var sum = data.reduce(function (a, b,i) {
    if (b[0][i]==b[0][i+1]) {
         return newData.push(b[0],a + b[1]);
    		 
    }    
 }, 0);
 console.log(newData);
 
 //return to:
 
[
["2013",23],
["2014",0],
["2015",2],
["2016",27],
["2017",5]
]

I want to sum the element value of the array, which have the same year.

var newData = [];
var data = new Array(['2013', 0], ['2013', 0],['2016', 15], ['2017', 2], ['2015', 1], ['2013', 0], ['2016', 12], ['2014', 0], ['2017', 3], ['2015', 1], ['2013', 21], ['2013', 2]);

var sum = data.reduce(function (a, b,i) {
    if (b[0][i]==b[0][i+1]) {
         return newData.push(b[0],a + b[1]);
    		 
    }    
 }, 0);
 console.log(newData);
 
 //return to:
 
[
["2013",23],
["2014",0],
["2015",2],
["2016",27],
["2017",5]
]

is it possible? Thank you for your help

Share Improve this question edited Feb 17, 2018 at 13:43 Redu 26.2k6 gold badges61 silver badges82 bronze badges asked Feb 17, 2018 at 12:28 Nirina-ajNirina-aj 2021 gold badge5 silver badges18 bronze badges 3
  • why not get result as an object where keys are years and values - sums? – RomanPerekhrest Commented Feb 17, 2018 at 12:31
  • wrote that on mobile, so i‘m not sure if it works. let s=data.reduce ((o,e)=>(o[e[0]]=~~o[e[0]]+e[1],o),{}); Object.keys(s).map(e=>[e,s[e]]) – Moritz Roessler Commented Feb 17, 2018 at 12:40
  • yeah,ok you're right – Nirina-aj Commented Feb 18, 2018 at 13:34
Add a ment  | 

4 Answers 4

Reset to default 4

You can use ES6 Map with reduce method to sum values by years and spread syntax ... to get array from Map.

var data = new Array(['2013', 0], ['2013', 0], ['2016', 15], ['2017', 2], ['2015', 1], ['2013', 0], ['2016', 12], ['2014', 0], ['2017', 3], ['2015', 1], ['2013', 21], ['2013', 2]);

const result = data.reduce((r, [y, v]) => {
  r.set(y, (r.get(y) || 0) + v);
  return r;
}, new Map)

console.log([...result]);

You can create your own simple and more understanding custom logic like this

To get JSON object as response.

var newData = [];
var data = new Array(['2013', 0], ['2013', 0],['2016', 15], ['2017', 2], ['2015', 1], ['2013', 0], ['2016', 12], ['2014', 0], ['2017', 3], ['2015', 1], ['2013', 21], ['2013', 2]);

var res = {};
data.forEach((item)=>{
  if(Object.keys(res).includes(item[0])){
    res[item[0]] += item[1];
  } else {
    res[item[0]] = item[1];
  }   
});
 console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }

To get JSON Array as response.

var newData = [];
var data = new Array(['2013', 0], ['2013', 0],['2016', 15], ['2017', 2], ['2015', 1], ['2013', 0], ['2016', 12], ['2014', 0], ['2017', 3], ['2015', 1], ['2013', 21], ['2013', 2]);

var res = [];
data.forEach((item)=>{
  var found = false;
  for(var i=0; i<res.length; i++){
    if(res[i][0] === item[0]){
       res[i][1] += item[1];
       found = true;
       break;
    }
  }
  if(!found){
    res.push(item);
  }
});
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: 0; }

If I understand what you need, then try this

var sums=data.reduce(function(a,b){
    a[b[0]]=(a[b[0]] || 0)+b[1];
},{});

One very fast way of doing this job is using the sparse arrays of JS and picking out the non-empty slots;

var data    = [['2013', 0], ['2013', 0],['2016', 15], ['2017', 2], ['2015', 1], ['2013', 0], ['2016', 12], ['2014', 0], ['2017', 3], ['2015', 1], ['2013', 21], ['2013', 2]],
    interim = data.reduce((r,e) => (r[+e[0]] && (r[+e[0]][1] += e[1]) || (r[+e[0]] = e), r), []),
    result  = Object.keys(interim)
                    .map(k => interim[k]);

console.log(result);
.as-console-wrapper {
height     : 100%;
max-height : 100% !important
}

So the Object.keys(interim) part just filters out the slots with undefined keys in a blazingly fast fashion. So nothing to worry there. Check this out;

var a = [],
    b = [];
a[1000000000] = "west";
a[500000000]  = "test";
a[0]          = "best";
console.time("test");
b = Object.keys(a)
          .map(k => a[k]);
console.timeEnd("test");
console.log(b);

发布评论

评论列表(0)

  1. 暂无评论