i have an array of objects like this:
arr = [
{name: "Alex", value: 1},
{name: "Bill", value: 2},
{name: "Jim", value: 3},
{name: "Dim", value: 5}
]
I want quick and clean way to have the min an max values of the value property
min = 1,
max = 5
You can use the reduce
function on an array for min:
arr.reduce(function(prev, curr) {
return prev.value < curr.value ? prev : curr;
});
max would work in a similar way
var min = Number.POSITIVE_INFINITY, max = Number.NEGATIVE_INFINITY; arr.forEach(function(obj) { min = Math.min(min, obj.value); max = Math.max(max, obj.value); });
You can use map()
to return array of values and then use Math.max/min
with spread syntax.
var arr = [{name:'Alex', value:1}, {name:'Bill', value:2},{name:'Jim', value: 3},{name: 'Dim', value:5}] var values = arr.map(e => e.value); var min = Math.min(...values); var max = Math.max(...values); console.log(max) console.log(min)
You could either map
the values and use Math
function to get highest and the lowest value.
var arr = [{name:'Alex', value:1}, {name:'Bill', value:2},{name:'Jim', value: 3},{name: 'Dim', value:5}], hash = arr.map(v => v.value), min = Math.min.apply(Math, hash), max = Math.max.apply(Math, hash); console.log(min, max);
Or just sort the array of object based on the value
property and choose the first and the last one.
var arr = [{name:'Alex', value:1}, {name:'Bill', value:2},{name:'Jim', value: 3},{name: 'Dim', value:5}], hash = arr.sort((a,b) => a.value - b.value); min = hash[0].value; max = hash[hash.length-1].value; console.log(min, max);
Most performant would probably be a simple loop
arr = [ {name: "Alex", value: 1}, {name: "Bill", value: 2}, {name: "Jim", value: 3}, {name: "Dim", value: 5} ] let min,max; for (var i=arr.length; i--;) { if ((!min) || arr[i].value < min) min = arr[i].value; if ((!max) || arr[i].value > max) max = arr[i].value; } console.log(min, max)
Shortest to write, something like
arr = [ {name: "Alex", value: 1}, {name: "Bill", value: 2}, {name: "Jim", value: 3}, {name: "Dim", value: 5} ] let m=Math,a=arr.map(z=>z.value);let [min,max]=[m.min(...a),m.max(...a)]; console.log(min,max)
You can create a function to do it for you, and every time you need to find the min/max, it only takes you one line:
function findMM (arr) {
var minV;
var maxV;
var firstLoop = true;
arr.forEach(function(object){
if (firstLoop) {
minV = object.value;
maxV = object.value;
firstLoop = false;
} else {
if (object.value < minV){ minV = object.value }
if (object.value > maxV){ maxV = object.value }
}
});
return {min: minV, max: maxV};
}
//using the function
var results = findMM(arr);
console.log(JSON.stringify(results)); //output: {"min":1,"max":5}
You could sort your array using native Array.prototype.sort()
and take the first and last value.
let data = [{ name: 'Ales', value: 1 }, { name: 'Dim', value: 5 }, { name: 'Bill', value: 2 }, { name: 'Jim', value: 3 } ]; data = data.sort((a, b) => a.value - b.value); let min = data [0].value; let max = data [data .length - 1].value; console.log('min:', min); console.log('max:', max)
Notes:
If you use instead:
let min = data [0];
let max = data [data .length - 1];
you could return the full object which has a minimum or maximum value, which could be handy.
Using reduce()
:
var arr = [{name:'Alex', value:1}, {name:'Bill', value:2},{name:'Jim', value: 3},{name: 'Dim', value:5}]
var result = arr.reduce(function(acc, val){
return { min: Math.min(acc.min, val.value),
max: Math.max(acc.max, val.value)}
}, {min: arr[0].value, max: arr[0].value});
console.log(result.max);
console.log(result.min);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.