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: 'test1@email.com',
duration: 60,
active: true
},
{
email: 'test2@email.com',
duration: 180,
active: false
}
],
},
{
name: 'User2',
configuration: [{
email: 'test3@email.com',
duration: 120,
active: true
},
{
email: 'test4@email.com',
duration: 30,
active: true
}
],
},
{
name: 'User3',
configuration: [{
email: 'test5@email.com',
duration: 300,
active: true
},
{
email: 'test6@email.com',
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