Compare JavaScript Array of Objects to Get Min/Max

Compare JavaScript Array of Objects to Get Min / Max

One way is to loop through all elements and compare it to the highest/lowest value.

(Creating an array, invoking array methods is overkill for this simple operation).

 // There's no real number bigger than plus Infinity
var lowest = Number.POSITIVE_INFINITY;
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
tmp = myArray[i].Cost;
if (tmp < lowest) lowest = tmp;
if (tmp > highest) highest = tmp;
}
console.log(highest, lowest);

Finding the max value of an attribute in an array of objects

To find the maximum y value of the objects in array:

    Math.max.apply(Math, array.map(function(o) { return o.y; }))

or in more modern JavaScript:

    Math.max(...array.map(o => o.y))

Compare JavaScript Array of Objects to Get Min / Max With 2 Properties At the Same Time

You could get an array of the reduced data set and then take a random object.

var items = [{ ID: 1, Cost: 200, Count: 4 }, { ID: 2, Cost: 1000, Count: 2 }, { ID: 3, Cost: 50, Count: 10 }, { ID: 4, Cost: 50, Count: 10 }, { ID: 5, Cost: 50, Count: 10 }, { ID: 6, Cost: 50, Count: 8 }],    result = items.reduce((r, o) => {        if (!r || r[0].Cost > o.Cost || r[0].Cost === o.Cost && r[0].Count < o.Count) {            return [o];        }        if (r[0].Cost === o.Cost && r[0].Count === o.Count) {            r.push(o);        }        return r;    }, null);

console.log(result[Math.floor(Math.random() * result.length)]); // randomconsole.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Javascript get object from array having min value and min value is needs to find and then find object

You can use Array reduce method:

var list = [{'id':4,'name':'nitin','group':'angularjs'},{'id':1,'name':'nitin','group':'angularjs'},{'id':2,'name':'nitin','group':'angularjs'},{'id':3,'name':'nitin','group':'angularjs'}];
var result = list.reduce(function(res, obj) { return (obj.id < res.id) ? obj : res;});
console.log(result);

How do you find Min & Max of a property in an array of objects in Javascript

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') );

Get max and min of object values from JavaScript array

Use this example

var lowest = Number.POSITIVE_INFINITY;
var highest = Number.NEGATIVE_INFINITY;
var tmp;
for (var i=myArray.length-1; i>=0; i--) {
tmp = myArray[i].Cost;
if (tmp < lowest) lowest = tmp;
if (tmp > highest) highest = tmp;
}
console.log(highest, lowest);

Filter array of objects with min and max value along with null check

Try this:

const data = [
{
id: '11se23-213',
name: 'Data1',
points: [ { x: 5, y: 1.1 }, { x: 6, y: 2.1 }, { x: 7, y: 3.1 }, { x: 8, y: 1.5 }, { x: 9, y: 2.9 }, { x: 10, y: 1.1 } ]
},
{
id: 'fdsf-213',
name: 'Data2',
points: [ { x: 5, y: 3.1 }, { x: 6, y: 4.1 }, { x: 7, y: 2.1 }, { x: 8, y: 0.5 }, { x: 9, y: 1.9 }, { x: 10, y: 1.4 } ]
}
];

const filter = (arr, min, max) =>
arr.map(e => ({
...e,
points: e.points.filter(({ y }) => (min === null || y >= min) && (max === null || y <= max))
}));

console.log('min=1, max=2', filter(data, 1, 2));
console.log('min=null, max=2', filter(data, null, 2));
console.log('min=1, max=null', filter(data, 1, null));
console.log('min=null, max=null', filter(data, null, null));

Find min/max values from object array exlcuding infinity

It might be easier to filter by isFinite, and then spread into Math.min or Math.max:

let result = [  {    "id": "Foo 1",    "result": Infinity  },  {    "id": "Foo 2",    "result": 240.77777777777777  },  {    "id": "Foo 3",    "result": 714.55  },  {    "id": "Foo 4",    "result": Infinity  },  {    "id": "Foo 5",    "result": 1314.55  },  {    "id": "foo 6",    "result": Infinity  },]
const numsOnly = result.map(({ result }) => result);const min = Math.min(...numsOnly.filter(isFinite));const max = Math.max(...numsOnly.filter(isFinite));console.log('min', min, 'max', max);

Find min, max in array with objects

You could call Math.min and Math.max, passing in a mapped array containing only the relevant values like this:

function endProp( mathFunc, array, property ) {
return Math[ mathFunc ].apply(array, array.map(function ( item ) {
return item[ property ];
}));
}

var maxY = endProp( "max", pts, "Y" ), // 8.389
minY = endProp( "min", pts, "Y" ); // 8.37256908416748


Related Topics



Leave a reply



Submit