Using flatMap and Math.max to get a certain field’s highest value from obj

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: '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="cabeafb9befb8aafa7aba3a6e4a9a5a7">[email protected]</a>',
                duration: 60,
                active: true
            },
            {
                email: '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bfcbdacccb8dffdad2ded6d391dcd0d2">[email protected]</a>',
                duration: 180,
                active: false
            }
        ],
    },
    {
        name: 'User2',
        configuration: [{
                email: '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deaabbadaaed9ebbb3bfb7b2f0bdb1b3">[email protected]</a>',
                duration: 120,
                active: true
            },
            {
                email: '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d29382e29691d38303c3431733e3230">[email protected]</a>',
                duration: 30,
                active: true
            }
        ],
    },
    {
        name: 'User3',
        configuration: [{
                email: '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f682938582c3b6939b979f9ad895999b">[email protected]</a>',
                duration: 300,
                active: true
            },
            {
                email: '<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c58495f581a6c49414d4540024f4341">[email protected]</a>',
                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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x