Split a string to find the number iin an Array of Objects, and return the highest number.

Split a string to find the number iin an Array of Objects, and return the highest number.

by Nirvanachain

JavaScript

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

var highestNum=0;
for (var i=0; i<myArray.length; i++) {
    var num = parseInt(myArray[i]['level'].substr(1));
    if (num > highestNum) {
        highestNum = num;
    }
};

console.log(highestNum);