A quick way to get min / max values ​​among object properties

I have an object in javascript:

{ "a":4, "b":0.5 , "c":0.35, "d":5 } 

Is there a quick way to get the minimum and maximum value among properties without having to scroll through them? because the object that I have is huge and I need to get the min / max value every two seconds. (The values ​​of the object continue to change).

+77
javascript jquery
Jun 21 '12 at 16:45
source share
10 answers

It is impossible to find the maximum / minimum in the general case without scrolling all the elements of n (if you go from 1 to n-1, how do you know if the element n will be no more (or less) than the current max / min)?

You mentioned that the values ​​change every two seconds. If you know exactly which values ​​are changing, you can start with your previous max / min values ​​and compare only with the new ones, but even then, if one of the values ​​that were changed was your old max / min, you may need skip them again.

Another alternative - again, only if the number of values ​​that change is small - is to store the values ​​in a structure such as a tree or a heap, and as new values ​​arrive, you should insert (or update) them appropriately. But whether you can do this is unclear based on your question.

+18
Jun 21 2018-12-12T00:
source share

Try this:

 var arr = Object.keys( obj ).map(function ( key ) { return obj[key]; }); 

and then:

 var min = Math.min.apply( null, arr ); var max = Math.max.apply( null, arr ); 

Demo version: http://jsfiddle.net/7GCu7/1/




Update: modern version (ES6 +)

 let obj = { a: 4, b: 0.5 , c: 0.35, d: 5 }; let arr = Object.values(obj); let min = Math.min(...arr); let max = Math.max(...arr); console.log( 'Min value: ${min}, max value: ${max}' ); 
+113
Jun 21 '12 at 16:56
source share

min and max should still pass through the input array - how else would they find the largest or smallest element?

So just a quick for..in will work fine.

 var min = Infinity, max = -Infinity, x; for( x in input) { if( input[x] < min) min = input[x]; if( input[x] > max) max = input[x]; } 
+9
Jun 21 2018-12-12T00:
source share

You can try:

 const obj = { a: 4, b: 0.5 , c: 0.35, d: 5 }; const max = Math.max.apply(null, Object.values(obj)); console.log(max) // 5 
+7
Feb 05 '19 at 16:34
source share
 // 1. iterate through object values and get them // 2. sort that array of values ascending or descending and take first, // which is min or max accordingly let obj = { 'a': 4, 'b': 0.5, 'c': 0.35, 'd': 5 } let min = Object.values(obj).sort((prev, next) => prev - next)[0] // 0.35 let max = Object.values(obj).sort((prev, next) => next - prev)[0] // 5 
+5
Jul 11 '17 at 11:11
source share

Using lodash library you can write shorter

 _({ "a":4, "b":0.5 , "c":0.35, "d":5 }).values().max(); 
+3
Jun 09 '18 at 14:49
source share

For nested structures of different depths, i.e. {node: {leaf: 4}, leaf: 1} , this will work (using lodash or underscore):

 function getMaxValue(d){ if(typeof d === "number") { return d; } else if(typeof d === "object") { return _.max(_.map(_.keys(d), function(key) { return getMaxValue(d[key]); })); } else { return false; } } 
+2
Jun 25 '16 at 21:07
source share

Here's a solution that allows you to return the key as well and does only one loop. It sorts object records (by value) and then returns the first and last.

In addition, it returns a sorted object that can replace an existing object, so future sorts will be faster, because it will already be partially sorted = better than O (n). It is important to note that objects maintain their order in ES6.

 const maxMinVal = (obj) => { const sortedEntriesByVal = Object.entries(obj).sort(([, v1], [, v2]) => v1 - v2); return { min: sortedEntriesByVal[0], max: sortedEntriesByVal[sortedEntriesByVal.length - 1], sortedObjByVal: sortedEntriesByVal.reduce((r, [k, v]) => ({ ...r, [k]: v }), {}), }; }; const obj = { a: 4, b: 0.5, c: 0.35, d: 5 }; console.log(maxMinVal(obj)); 

+1
Dec 07 '18 at 1:08
source share

This works for me:

 var object = { a: 4, b: 0.5 , c: 0.35, d: 5 }; // Take all value from the object into list var valueList = $.map(object,function(v){ return v; }); var max = valueList.reduce(function(a, b) { return Math.max(a, b); }); var min = valueList.reduce(function(a, b) { return Math.min(a, b); }); 
0
May 26 '17 at 12:46 a.m.
source share

You can also try with Object.values

 const points = { Neel: 100, Veer: 89, Shubham: 78, Vikash: 67 }; const vals = Object.values(points); const max = Math.max(...vals); const min = Math.min(...vals); console.log(max); console.log(min); 

0
Aug 17 '19 at 18:03
source share



All Articles