Array.prototype.reduce() is good for stuff like this: to perform aggregate operations (like min, max, avg, etc.) on an array, and return a single result:
myArray.reduce(function(prev, curr) {
return prev.Cost < curr.Cost ? prev : curr;
});
...or you can define that inner function with ES6 function syntax:
myArray.reduce((prev, curr) => prev.Cost < curr.Cost ? prev : curr);
If you want to be cute you can attach this to the Array prototype:
Array.prototype.hasMin = function(attrib) {
return (this.length && this.reduce(function(prev, curr){
return prev[attrib] < curr[attrib] ? prev : curr;
})) || null;
}
Now you can just say:
myArray.hasMin('ID') // result: {"ID": 1, "Cost": 200}
myArray.hasMin('Cost') // result: {"ID": 3, "Cost": 50}
myEmptyArray.hasMin('ID') // result: null
Please note that if you intend to use this, it doesn't have full checks for every situation. If you pass in an array of primitive types, it will fail. If you check for a property that doesn't exist, or if not all the objects contain that property, you will get the last element. This version is a little more bulky, but has those checks:
Array.prototype.hasMin = function(attrib) {
const checker = (o, i) => typeof(o) === 'object' && o[i]
return (this.length && this.reduce(function(prev, curr){
const prevOk = checker(prev, attrib);
const currOk = checker(curr, attrib);
if (!prevOk && !currOk) return {};
if (!prevOk) return curr;
if (!currOk) return prev;
return prev[attrib] < curr[attrib] ? prev : curr;
})) || null;
}
Answer from Tristan Reid on Stack OverflowArray.prototype.reduce() is good for stuff like this: to perform aggregate operations (like min, max, avg, etc.) on an array, and return a single result:
myArray.reduce(function(prev, curr) {
return prev.Cost < curr.Cost ? prev : curr;
});
...or you can define that inner function with ES6 function syntax:
myArray.reduce((prev, curr) => prev.Cost < curr.Cost ? prev : curr);
If you want to be cute you can attach this to the Array prototype:
Array.prototype.hasMin = function(attrib) {
return (this.length && this.reduce(function(prev, curr){
return prev[attrib] < curr[attrib] ? prev : curr;
})) || null;
}
Now you can just say:
myArray.hasMin('ID') // result: {"ID": 1, "Cost": 200}
myArray.hasMin('Cost') // result: {"ID": 3, "Cost": 50}
myEmptyArray.hasMin('ID') // result: null
Please note that if you intend to use this, it doesn't have full checks for every situation. If you pass in an array of primitive types, it will fail. If you check for a property that doesn't exist, or if not all the objects contain that property, you will get the last element. This version is a little more bulky, but has those checks:
Array.prototype.hasMin = function(attrib) {
const checker = (o, i) => typeof(o) === 'object' && o[i]
return (this.length && this.reduce(function(prev, curr){
const prevOk = checker(prev, attrib);
const currOk = checker(curr, attrib);
if (!prevOk && !currOk) return {};
if (!prevOk) return curr;
if (!currOk) return prev;
return prev[attrib] < curr[attrib] ? prev : curr;
})) || null;
}
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);
Videos
You can use lodash's methods
_.max([4, 2, 8, 6]);
returns => 8
https://lodash.com/docs/4.17.15#max
_.min([4, 2, 8, 6]);
returns => 2
https://lodash.com/docs/4.17.15#min
Finding the Max and Min elements of an array in JavaScript.
There are several approaches you can use:
Using Math.min() and Math.max()
let array = [100, 0, 50];
Math.min(...array); // 0
Math.max(...array); // 100
Using Sorting
let array = [100, 0, 50];
arraySorted = array.toSorted((a, b) => a - b); // [0, 50, 100];
arraySorted.at(0); // 0
arraySorted.at(-1); // 100
Using simple for-loop
let array = [100, 0, 50];
let maxNumber = array[0];
let minNumber = array[0];
for (let i = 1; i < array.length; i++) {
if (array[i] > maxNumber) {
maxNumber = array[i];
}
if (array[i] < minNumber) {
minNumber = array[i];
}
}
Not sure if this sounds weird or if my approach is completely wrong.
I have an array with objects inside, and I want to check which object has the lowest value at a specific "name" (I hope that is the correct term).
myArray = [
{This: "A", That: "B", Number: 2},
{This: "C", That: "D", Number: 1},
{This: "E", That: "F", Number: 3}
]In this case I want to check which object in my array has the lowest number. So the result would be {This: "C", That: "D", Number: 1}
I need this to still know what other values are in this object. So I can't just compare the last bit of the object and just have 1 as an answer. I need the whole object.
Hello,
I would like to get the minimum date of this array of objects bellow:
let activities = [
{ title: 'Hiking', date: '2019-06-13' },
{ title: 'Shopping', date: '2019-06-10' },
{ title: 'Trekking', date: '2019-06-22' },
{ title: 'Trekking', date: null }
]
let sortedActivities = activities.sort((a, b) => new Date(a.date) - new Date(b.date))
console.log(sortedActivities[0])The problem is when date activities.date is null it always returns null .
What's the best way to reject null value from this comparison ?
Thanks