[Vuejs]-Js group array by value

0👍

First, let’s group elements of the same type in an associative array. Then form the answer in the desired form.

var a = [ [ [ "Mangas", 23809.132685271947 ], [ "Magazines", 2162.858571621124 ], [ "Journal", 0 ], [ "Livres", 2533.654678438289 ] ], [ [ "Mangas", 25809.508799386324 ], [ "Magazines", 2519.527899187236 ], [ "BDs", 0 ], [ "Livres", 2436.7144208655627 ] ] ];

let c = {};
for (let a1 of a) {
    for (let a2 of a1) {
        if (c[a2[0]]) {
            c[a2[0]].push(a2);
        } else {
            c[a2[0]] = [a2];
        }
    }
}
let d = [];
for (let c1 in c) {
    d.push(c[c1]);
}
console.log(d);

Leave a comment