Home > Blockchain >  Using flatMap and Math.max to get a certain field's highest value from obj
Using flatMap and Math.max to get a certain field's highest value from obj

Time:03-06

I am currently working with an object and trying to extract the highest value of duration field. I am trying to use both flatMap and Math.max to achieve this but so far I am getting a -Infinity result. I am also calling this flatMap twice which is likely not right :/. What would be the right approach to get the highest value of duration?

const users = [{
        name: 'User1',
        configuration: [{
                email: '[email protected]',
                duration: 60,
                active: true
            },
            {
                email: '[email protected]',
                duration: 180,
                active: false
            }
        ],
    },
    {
        name: 'User2',
        configuration: [{
                email: '[email protected]',
                duration: 120,
                active: true
            },
            {
                email: '[email protected]',
                duration: 30,
                active: true
            }
        ],
    },
    {
        name: 'User3',
        configuration: [{
                email: '[email protected]',
                duration: 300,
                active: true
            },
            {
                email: '[email protected]',
                duration: 10,
                active: true
            }
        ],
    },
];

Code:

const x = users.flatMap(user => user.configuration);
const y = x.flatMap(user => user.duration);
const highestvalue = Math.max(...Object.values(y).flat().flatMap(Object.values));
console.log(highestvalue);

Current Result:

-Infinity

Desired Result:

300

CodePudding user response:

With your current code, your y array contains the list of destination values. You can use Math.max(...y) to get the max number from that array. While you're calling .flatMap() twice, you only need to call it once on your original users array to flatten the objects within your configuration arrays into one resulting array. Once you have these objects flattened you don't need to call it again when you map x and instead can use just .map():

const users = [{ name: 'User1', configuration: [{ email: '[email protected]', duration: 60, active: true }, { email: '[email protected]', duration: 180, active: false } ], }, { name: 'User2', configuration: [{ email: '[email protected]', duration: 120, active: true }, { email: '[email protected]', duration: 30, active: true } ], }, { name: 'User3', configuration: [{ email: '[email protected]', duration: 300, active: true }, { email: '[email protected]', duration: 10, active: true } ], }, ];

const x = users.flatMap(user => user.configuration);
const y = x.map(user => user.duration);
console.log(Math.max(...y));

You can also simplify by removing the intermediate variables by mapping each object which each configuration array to its duration, and which then gets flattened into one resulting array due to the outer .flatMap():

const users = [{ name: 'User1', configuration: [{ email: '[email protected]', duration: 60, active: true }, { email: '[email protected]', duration: 180, active: false } ], }, { name: 'User2', configuration: [{ email: '[email protected]', duration: 120, active: true }, { email: '[email protected]', duration: 30, active: true } ], }, { name: 'User3', configuration: [{ email: '[email protected]', duration: 300, active: true }, { email: '[email protected]', duration: 10, active: true } ], }, ];

const highest = Math.max(...users.flatMap(
  user => user.configuration.map(({duration}) => duration)
));

console.log(highest);


Note, as @Chris_F pointed out, if you expect your array to be large or are unsure of the size of your array, then you may potentially run into the max argument limitation when trying to spread your array elements into Math.max(). In this case, you can use a loop to find the max value (rather than spreading the arrary elements), below I've used .reduce():

const max = arr => arr.reduce((m, c) => Math.max(m, c), -Infinity);

const users = [{ name: 'User1', configuration: [{ email: '[email protected]', duration: 60, active: true }, { email: '[email protected]', duration: 180, active: false } ], }, { name: 'User2', configuration: [{ email: '[email protected]', duration: 120, active: true }, { email: '[email protected]', duration: 30, active: true } ], }, { name: 'User3', configuration: [{ email: '[email protected]', duration: 300, active: true }, { email: '[email protected]', duration: 10, active: true } ], }, ];

const highest = max(users.flatMap(
  user => user.configuration.map(({duration}) => duration)
));

console.log(highest);

  • Related