Skip to content Skip to sidebar Skip to footer

Finding Maximum Value In An Array Of Objects Where Numer Is A Part Of String, Most Optimal Solution And Write Tests

I have this code which finding max value from object array where number is part of string: var stringArray = [ { name: 'string 1' }, { name: 'string 2' }, { name: 'string 11' }, {

Solution 1:

You can use Math.max with spread syntax and map() method.

var stringArray = [
{ name: 'string 1' },
{ name: 'string 2' },
{ name: 'string 11' },
{ name: 'string 3' },
{ name: 'string 10' }
];

var max = Math.max(...stringArray.map(e => e.name.match(/\d+$/)))
console.log(max)

Post a Comment for "Finding Maximum Value In An Array Of Objects Where Numer Is A Part Of String, Most Optimal Solution And Write Tests"