Search code examples
javascriptjavascript-objects

How to get max property value in an object with duplicate property name?


Suppose I have an array with the following values:

Array name is "subscribers"

1: {month: "2019-07-24", subs: 2}
2: {month: "2019-07-31", subs: 3}
3: {month: "2019-08-01", subs: 2}
4: {month: "2019-08-02", subs: 3}
5: {month: "2019-08-05", subs: 3}
6: {month: "2019-08-08", subs: 4}
7: {month: "2019-08-14", subs: 5}
8: {month: "2019-08-20", subs: 7}
9: {month: "2019-08-23", subs: 7}
10: {month: "2019-08-28", subs: 8}
11: {month: "2019-08-29", subs: 11}
12: {month: "2019-09-02", subs: 2}
13: {month: "2019-09-03", subs: 2}
14: {month: "2019-09-04", subs: 3}
15: {month: "2019-09-05", subs: 5}
16: {month: "2019-09-06", subs: 5}
17: {month: "2019-09-09", subs: 6}
18: {month: "2019-09-10", subs: 7}
19: {month: "2019-09-11", subs: 8}
20: {month: "2019-09-12", subs: 9}

My question is, how can I get the highest subs by monthly?

Expected output:

0: {month: "July", subs: 3}
1: {month: "August", subs: 11}
2: {month: "September", subs: 9}

I tried using array.map, but I cannot get the expected output. Also I tried using Math.max, but I cannot seem to use it in an object.

var getMonthlyValues = subscribers.map(value => {   
        var obj = {};
        var dateName = new Date(value.period).toLocaleString('default', { month: 'long' });
        obj = {month:dateName, subs:value.monthly}
        return obj;
    })

I can do this if for example these are from database with columns as month and sub, with their respective values. But how can I do this on pure JS?


Solution

  • You could take a Map, and collect the maximum subs for every month.

    In steps:

    • Reduce the array by taking a Map as accumulator and take the grouping property month as key. As value take the maximum from either a previously stored value or zero and the actual value.

      If negative values or zeros are available use a conditional (ternary) operator ?:, like

        m.has(month) ? Math.max(m.get(month), subs) : subs
      
    • Take the list of key/value pairs and return an array of objects with custom named properties with Array.from and a mapping function.

    var data = [{ month: "July", subs: 2 }, { month: "July", subs: 3 }, { month: "August", subs: 2 }, { month: "August", subs: 3 }, { month: "August", subs: 3 }, { month: "August", subs: 4 }, { month: "August", subs: 5 }, { month: "August", subs: 7 }, { month: "August", subs: 7 }, { month: "August", subs: 8 }, { month: "August", subs: 11 }, { month: "September", subs: 2 }, { month: "September", subs: 2 }, { month: "September", subs: 3 }, { month: "September", subs: 5 }, { month: "September", subs: 5 }, { month: "September", subs: 6 }, { month: "September", subs: 7 }, { month: "September", subs: 8 }, { month: "September", subs: 9 }],
        result = Array.from(
            data.reduce(
                (m, { month, subs }) => m.set(month, Math.max(m.get(month) || 0, subs)),
                new Map
            ),
            ([month, subs]) => ({ month, subs })
        );
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }