Skip to content

Instantly share code, notes, and snippets.

@rao123dk
Created June 7, 2018 11:25
Show Gist options
  • Save rao123dk/6015109e1c6c60966968a1c255e7de2a to your computer and use it in GitHub Desktop.
Save rao123dk/6015109e1c6c60966968a1c255e7de2a to your computer and use it in GitHub Desktop.
Find the highest number in array of objects.
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;}))
}
@rao123dk
Copy link
Author

rao123dk commented Jun 7, 2018

It will return the longest name.
var _get_max_name = array.reduce((prev, current) => (prev.name.length > current.name.length) ? prev : current);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment