Home > Back-end >  How generate uniques grouping/summing one of the columns based on 2 criteria in Javascript?
How generate uniques grouping/summing one of the columns based on 2 criteria in Javascript?

Time:06-25

I've been trying to get this one, but it's still hard for me.

The data:

let data = [
 [1, "Item A", "Food", 10],
 [2, "Item B", "Food", 5],
 [3, "Item C", "Food", 30],
 [4, "Item A", "Hygiene", 30],
 [4, "Item A", "Hygiene", 50],
 [6, "Item D", "Food", 7],
 [3, "Item C", "Food", 8],
 [1, "Item A", "Food", 60],
]

Result

let result = [
 [1, "Item A", "Food", 70],
 [2, "Item B", "Food", 5],
 [3, "Item C", "Food", 38],
 [4, "Item A", "Hygiene", 80],
 [6, "Item D", "Food", 7],
]

This is basic reduce() which I imagine would be the path to take, but how to set the criteria e generate unique rows?

const array1 = [1, 2, 3, 4];
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue, currentValue) => previousValue   currentValue,
  initialValue
);
console.log(sumWithInitial);

Appreciate your help!

CodePudding user response:

You could take a combined key for grouping.

const
    data = [[1, "Item A", "Food", 10], [2, "Item B", "Food", 5], [3, "Item C", "Food", 30], [4, "Item A", "Hygiene", 30], [4, "Item A", "Hygiene", 50], [6, "Item D", "Food", 7], [3, "Item C", "Food", 8], [1, "Item A", "Food", 60]],
    result = Object.values(data.reduce((r, a) => {
        const key = [0, 1].map(i => a[i]).join('|');
        if (r[key]) r[key][3]  = a[3];
        else r[key] = [...a];
        return r;
    }, {}));

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

CodePudding user response:

I've used map with findIndex
I used also reverse(), Object.values with Object.fromEntries methods to get unique ones:

let data = [
 [1, "Item A", "Food", 10],
 [2, "Item B", "Food", 5],
 [3, "Item C", "Food", 30],
 [4, "Item A", "Hygiene", 30],
 [4, "Item A", "Hygiene", 50],
 [6, "Item D", "Food", 7],
 [3, "Item C", "Food", 8],
 [1, "Item A", "Food", 60],
]
let objs = data.slice(); //in order to not mutate the source array data
let sumObjs = objs.map((curr, i) => {
let indexPrev = objs.findIndex(
    (item) => item[1] == curr[1] && item[2] == curr[2]
);
if (indexPrev != -1 && i != indexPrev) {
    //i != indexPrev to not accumelate the current one
    //indexPrev =! -1 means that one is exist
    objs[indexPrev][3]  = curr[3];
}
return curr;
});
let result = Object.values(
Object.fromEntries(
    sumObjs.reverse().map((obj) => [JSON.stringify(obj[1] obj[2]), obj])
)
);
console.log(result);

  • Related