Return highest number found in an array of strings

Posted by Mdd on Stack Overflow See other posts from Stack Overflow or by Mdd
Published on 2014-06-09T14:36:45Z Indexed on 2014/06/09 15:25 UTC
Read the original article Hit count: 271

Filed under:
|

I have an array of objects. The objects have a property called level that is a string which contains a number and is in consistent format.

I am trying to find the highest number and return just that one but I seem to be blocked as far as how to proceed from my current code.

Here is a fiddle to my current code: http://jsfiddle.net/6sXYR/

Here is my JavaScript:

var myArray = [
    {
        color: 'blue',
        level: 'L1'
    },
    {
        color: 'red',
        level: 'L1'
    },
    {
        color: 'green',
        level: 'L2'
    },
    {
        color: 'yellow',
        level: 'L2'
    },
    {
        color: 'purple',
        level: 'L3'
    }
];

for (var i = 0; i < myArray.length; i++) {
    console.log( myArray[i].level.substring(1,2) );
};

The result I am trying to get is to return just the number '3' from the example above. The highest number may not always be in the last object in the array, but it will always be in the format of L#.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery