# Find min/max values from object array exlcuding infinity

52
May 18, 2019, at 4:40 PM

I have an array of objects where I want to find the min/max value of a property in each object. I currently have a working method to find the min, however I am struggling with the max, if the value is Infinity I want to ignore it.

``````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
},
]

let minCount = result.reduce(function(max, obj) {
return obj.result < max.result ? obj : max;
});
console.log("Min", minCount.result);
let maxCount = result.reduce(function(max, obj) {
return obj.result > max.result ? obj : max;
});
console.log("Max", maxCount.result);``````

This code works fine for unless I have an infinite value as `result` I've tried setting the return to `return isFinite(obj.result) > max.result ? obj : max;` However once it gets to the first Infinit value it will get the value before it, and stop looking for a max value in the rest of the function.

I've similarly tried `return obj.result > isFinite(max.result) ? obj : max;` Which does the opposite and finds the last value and not regard if its infinity.

If it makes things easier I can change the infinite value to `undefined` or another primitive, however I can't have these values affecting my Min/Max calculation.

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

If you need the objects as well as the max/min values, filter the objects by `isFinite` first:

``````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 finiteObjs = result.filter(({ result }) => isFinite(result));
const minObj = finiteObjs.reduce((a, b) => a.result < b.result ? a : b);
const maxObj = finiteObjs.reduce((a, b) => a.result > b.result ? a : b);
console.log('minObj', minObj, 'maxObj', maxObj);``````

Check with this

``````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
},
]

let min=Number.MAX_VALUE;
let max=Number.MIN_VALUE;

result.forEach(item => {
min = isFinite(item.result) ? Math.min(item.result,min): min;

max = isFinite(item.result) ? Math.max(item.result,max): max;

});

console.log('Min:', min);
console.log('Max:',max);``````

It only works if your array contains at least one min and Max value ( Finite values)

You can slightly change your comparison logic and get it working. Have a look at this:

``````const max = result.reduce((a,b)=>{
return a > b.result && isFinite(a) ? a : (isFinite(b.result) ? b.result : a);
},0)
``````
POPULAR ONLINE

### How to sort array of objects by day, if object can includes 2 or more timestamp, if no same date - create new property & set to null

I have array of objects where each element has 2 or more unique keys 'datetime' and also i have data that belongs to the each datetimeI need sort by same date(by day), if no date we should set value to null

33

### loop inner object array in angular without having iteration of outer array [on hold]

I am web service which return nested object arrayIn my angular example, I have iterated the inner array ie currencies in users

36

### Ionic 4: No emulator images (avds) found

I'm trying to emulate my ionic 4 app into Android Emulator so I run the command:

51

### Do you know any frontend formula creator component to use in html app? [on hold]

We are building financial single page application using angularWe need to build a UI for user to create formula like how we do in excel

44