Datacrawler
Datacrawler

Reputation: 2876

Group-by an array of array of objects

I am grouping by an array of objects depending on the value of the first item below:

const json2 = [{"value":"value1","metric":1},{"value":"value1","metric":2},{"value":"value3","metric":0},{"value":"value2","metric":4},{"value":"value3","metric":1},{"value":"value3","metric":1}];

const result2 = [...json2.reduce((r, o) => {
  const key = o.value;
  
  const item = r.get(key) || Object.assign({}, o, {
    metric: 0,
  });
  
  item.metric += o.metric;

  return r.set(key, item);
}, new Map).values()];

console.log(result2);

but I am struggling to do the same when I have an array of array of objects as shown below:

[[{"value":"value1","formattedValue":"value1"},{"value":2831.8,"formattedValue":"283,180.00 %"}],[{"value":"value1","formattedValue":"value1"},{"value":349.1111111111111,"formattedValue":"34,911.11 %"}],[{"value":"value2","formattedValue":"value2"},{"value":3.3703703703703702,"formattedValue":"337.04 %"}]]

So, comparing to the previous JSON the difference is that this:

{"value":"value1","metric":1}

is now:

[{"value":"value1","formattedValue":"value1"},{"value":"1.0","formattedValue":100.00 %}]

A solution would be to manipulate the second query to make it look like the first one but in this case I want to make it work with the function above.

Wanted result is:

[[{"value":"value1","formattedValue":"value1"},{"value":3180.91111111,"formattedValue":"318091.11 %"}],[{"value":"value2","formattedValue":"value2"},{"value":3.3703703703703702,"formattedValue":"337.04 %"}]]

Upvotes: 1

Views: 88

Answers (1)

Nina Scholz
Nina Scholz

Reputation: 386570

You could take the key/value by a destructuring and update the result set.

var data = [[{ value: "value1", formattedValue: "value1" }, { value: 2831.8, formattedValue: "283,180.00 %" }], [{ value: "value1", formattedValue: "value1" }, { value: 349.1111111111111, formattedValue: "34,911.11 %" }], [{ value: "value2", formattedValue: "value2" }, { value: 3.3703703703703702, formattedValue: "337.04 %" }]],
    result = Array.from(
        data
            .reduce((m, a) => {
                var [{ value: key }, { value }] = a,
                    target = m.get(key);

                if (!target) return m.set(key, JSON.parse(JSON.stringify(a)));
                target[1].value += value;
                target[1].formattedValue = `${(target[1].value * 100).toFixed(2) } %`;
                return m;
                
            }, new Map)
            .values()                
    );        

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

Upvotes: 3

Related Questions