I have the following javascript object:
Person1.Name = "John";
Person1.Age = 12;
Person2.Name = "Joe";
Person2.Age = 5;
I then have an array of persons, how do I find the Min/Max based on a persons age?
Any solution in Javascript or Jquery is acceptable.
your help is much appreciated.
"Min" means minimum, so in your case it's saying what's the minimum of the two values in (c-a, b-c). Cite.
A minimum value is the lowest y-value of a function. A maximum value is the highest y-value of a function.
The local minima and maxima can be found by solving f'(x) = 0. Then using the plot of the function, you can determine whether the points you find were a local minimum or a local maximum. Also, you can determine which points are the global extrema. Not all functions have a (local) minimum/maximum.
Say your array looks like this:
var persons = [{Name:"John",Age:12},{Name:"Joe",Age:5}];
then you can:
var min = Math.min.apply(null, persons.map(function(a){return a.Age;}))
,max = Math.max.apply(null, persons.map(function(a){return a.Age;}))
[Edit] Added ES2015 method:
const minmax = (someArrayOfObjects, someKey) => {
const values = someArrayOfObjects.map( value => value[someKey] );
return {
min: Math.min.apply(null, values),
max: Math.max.apply(null, values)
};
};
console.log(
minmax(
[ {Name: "John", Age: 12},
{Name: "Joe", Age: 5},
{Name: "Mary", Age: 3},
{Name: "James sr", Age: 93},
{Name: "Anne", Age: 33} ],
'Age')
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With