Summing a 2D array by “group”

为君一笑 提交于 2021-02-17 07:20:10

问题


I have an array in the form

[
['16-24', 192081],
['16-24', 94452],
['16-24', 1055],
['25-34', 192081],
['25-34', 94452],
['25-34', 1055],
...
]

I have many items in the array that I'd like to reduce down to the unique left-hand entries. This is an example of what I'd like to reduce down to

[
['16-24', 287588],
['25-34', 287588],
...
]

Obviously the numbers here are a contrived example. I've looked into javascript's array reduce but I'm unsure how to use it to achieve this effect


回答1:


Use reduce() to sum and map() to change it to the format you want.

var vals = [
['16-24', 192081],
['16-24', 94452],
['16-24', 1055],
['25-34', 192081],
['25-34', 94452],
['25-34', 1055]
];

var temp = vals.reduce(function (obj, cur) {
    var total = obj[cur[0]] || 0;
    obj[cur[0]] = total + cur[1];
    return obj;
}, {} );

var result = Object.keys(temp).map( function (key) { return [key, temp[key]]; });
console.log(result);                                                             



回答2:


Here is an approach which calculates the sums "in-place", avoiding the necessity of having to create a separate object with reduce etc., then converting the object back into an array.

The basic plan is to add to the first occurrence of each key the numbers from subsequent occurrences, then filter out those subsequent occurrences.

function sum(a) {
  return a.filter(([key, num], idx) => {
    var first = a.findIndex(([key2]) => key === key2);
    if (first === idx) return true;
    a[first][1] += num;
  });
}

For each tuple, we use findIndex to find the first tuple with the same key. By comparing that index with the current index, we can tell if the current tuple is the first occurrence. If it is, we leave it alone (return true;); otherwise, we filter it out, but first add its value to the first occurrence. This avoids the necessity of having to create an object with reduce etc., then converting the object back into an array.

The possibly unfamiliar syntax ([key, num], idx) is function parameter destructuring. It takes the first parameter, assumes it's an array, and assigns the first element to key and the second element to num. This is a bit cleaner than referring to a[0] and a[1] from within the code.



来源:https://stackoverflow.com/questions/34678309/summing-a-2d-array-by-group

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!