Created
June 7, 2018 11:25
-
-
Save rao123dk/6015109e1c6c60966968a1c255e7de2a to your computer and use it in GitHub Desktop.
Find the highest number in array of objects.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array = [ | |
{"name":"dheeraj"}, | |
{"name":"download"}, | |
{ "name":"code"}, | |
{"name":"bookbookbookbook"}, | |
{"name":"book"} | |
]; | |
function getMax() { | |
return array.reduce((max, p) => p.name.length > max ? p.name.length : max, array[0].name.length); | |
} | |
//OR | |
function getMax() { | |
return Math.max.apply(Math,array.map(function(o){return o.name.length;})) | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It will return the longest name.
var _get_max_name = array.reduce((prev, current) => (prev.name.length > current.name.length) ? prev : current);