1

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 %"}]]
2
  • 1
    please add the wanted result. Commented Sep 5, 2019 at 11:17
  • 1
    I removed the JSON tag as there was nothing about JSON in there, just JavaScript objects with names like json2 Commented Sep 5, 2019 at 12:37

1 Answer 1

3

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; }

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.