Ardi
Ardi

Reputation: 161

How to insert conditional statement in Object Map?

in follow up of this question:

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

I want to insert conditional statement if the current iteration is equal to a year

I tried doing it this way:

var monthlyHighest = Array.from(
    getMonthlyValues.reduce(
     (m, {month, subs, year}) => {
      if(year == 2018){ //here is the conditional statement
        return m.set(month, Math.max(m.get(month) || 0, subs))
      }
     },new Map),
     ([month, subs, year]) => ({ month, subs, year}));

But I get a "Cannot convert undefined or null to object" error, which in my understanding, if the line if(year == 2018) returned false, the whole reduce function will not proceed anymore.

I used the following array for testing:

1: {month: "2018-07-24", subs: 2}
2: {month: "2018-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}

Upvotes: 0

Views: 189

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

I take it the data you actually work with has objects with {year, month, subs} where month is just the month?

This works: note the hardcoded year in

([month, subs, year]) => ({ month, subs, year:2018})

because a Map will only have 2 elements in the iterable used by Array.from - not sure where you expected the year to come from

const getMonthlyValues = [,
    {month: "2018-07-24", subs: 2},
    {month: "2018-07-31", subs: 3},
    {month: "2019-08-01", subs: 2},
    {month: "2019-08-02", subs: 3},
    {month: "2019-08-05", subs: 3},
    {month: "2019-08-08", subs: 4},
    {month: "2019-08-14", subs: 5},
    {month: "2019-08-20", subs: 7},
    {month: "2019-08-23", subs: 7},
    {month: "2019-08-28", subs: 8},
    {month: "2019-08-29", subs: 11},
    {month: "2019-09-02", subs: 2},
    {month: "2019-09-03", subs: 2},
    {month: "2019-09-04", subs: 3},
    {month: "2019-09-05", subs: 5},
    {month: "2019-09-06", subs: 5},
    {month: "2019-09-09", subs: 6},
    {month: "2019-09-10", subs: 7},
    {month: "2019-09-11", subs: 8},
    {month: "2019-09-12", subs: 9},
].map(v => ({year: v.month.split('-')[0], month: v.month.split('-')[1], subs:v.subs}));

var monthlyHighest = Array.from(
    getMonthlyValues.reduce((m, {month, subs, year}) => {
        if(year == 2018){ //here is the conditional statement
            m.set(month, Math.max(m.get(month) || 0, subs))
        }
        return m;
    }, new Map),
    ([month, subs, year]) => ({ month, subs, year:2018})
);
console.log(monthlyHighest);

Upvotes: 1

Related Questions