Finding Object With Lowest Value For Some Key, In Javascript
In my Javascript program, I have a list of Person objects. For example [ 'Michael': { 'age': 45, 'position': 'manager', ... }, 'Dwight':
Solution 1:
You can sort the persons array by age property and pick first one:
var persons = [
{ name: 'Michael', age: 45, position: 'manager' },
{ name: 'Dwight', age: 36, position: 'assistant manager' },
{ name: 'Foo', age: 99, position: 'foo' },
{ name: 'Bar', age: 37, position: 'bar' }
];
persons.sort(function(a, b) {
return a.age > b.age;
});
console.log('Youngest person: ', persons[0]);
Post a Comment for "Finding Object With Lowest Value For Some Key, In Javascript"