Hi how to get object with largest Price using javascript:
I have this code but it doesn't work good
var array = [{Name: "blue",Price: 20},{Name: "red",Price: 10},{Name: "green",Price: 30}];
var biggestNumber = 0;
for (var i = 0; i < array.length; i++){
var item = array[i]
if(biggestNumber < item.Price) {
biggestNumber = item;
}
}
console.log(biggestNumber);
let highestPrice = Math.max.apply(null, array.map(e => e.Price));
let item = array.find(e => e.Price === highestPrice);
var array = [{Name: "blue",Price: 20},{Name: "red",Price: 10},{Name: "green",Price: 30}]; var maxIndex = 0; // assume the max is at 0 for(var i = 1; i < array.length; i++) { // for the rest of the objects in the array if(array[i].Price > array[maxIndex].Price) // if the current object is bigger than the assumed max maxIndex = i; // then this current object's index is the new maxIndex } var maxObject = array[maxIndex]; // max object is the object at maxIndex console.log(maxObject);
You can use Array.prototype.reduce
to "reduce" your entire array into a single value: the object with the largest price.
var itemWithHighestPrice = array.reduce(function(highest, item, index) {
if (index === 0 || item.Price > highest.Price) {
return item;
}
return highest;
}, {});
You have to store item.Price in biggestNumber instead of just item.
var array = [{Name: "blue",Price: 20},{Name: "red",Price: 10},{Name: "green",Price: 30}];
var biggestNumber = 0;
for (var i = 0; i < array.length; i++){
var item = array[i]
if(biggestNumber < item.Price) {
biggestNumber = item.Price;
}
}
console.log(biggestNumber);
Classic approach with for
statement .
This proposal takes the first element as temporary result and checks all following objects of the array with the result. If a greater Price
is found, then result
is changed to the actual element.
var array = [{ Name: "blue", Price: 20 }, { Name: "red", Price: 10 }, { Name: "green", Price: 30 }], result = array[0], // take the first element i; for (i = 1; i < array.length; i++) { // iterate from the second on if (array[i].Price > result.Price) { // check price, if a greater price found result = array[i]; // replace result with actual object } } console.log(result);
Or you could use Array#reduce
and return the result of the check inside of the callback.
var array = [{ Name: "blue", Price: 20 }, { Name: "red", Price: 10 }, { Name: "green", Price: 30 }], result = array.reduce(function (a, b) { return a.Price > b.Price ? a : b; }, {}); console.log(result);
The same as above with ES6
var array = [{ Name: "blue", Price: 20 }, { Name: "red", Price: 10 }, { Name: "green", Price: 30 }], result = array.reduce((a, b) => a.Price > b.Price ? a : b, {}); console.log(result);
var array = [{Name: "blue",Price: 20},{Name: "red",Price: 10},{Name: "green",Price: 30}]; var biggestNumber = {Price:0}; array.forEach(function(obj){ if(obj.Price>biggestNumber.Price) biggestNumber=obj; }); console.log(biggestNumber);
Problem:
Your problem is that you are storing the whole item
object in your highestPrice
variable, then you try to compare a Number
with an object
.
Solution:
You should store only the item.Price
in your highestPrice
variable and compare Price
values, edit your code like this:
var biggestNumber = 0;
for (var i = 0; i < array.length; i++) {
var item = array[i];
if (biggestNumber < item.Price) {
biggestNumber = item.Price;
}
}
Working snippet:
var array = [{ Name: "blue", Price: 20 }, { Name: "red", Price: 10 }, { Name: "green", Price: 30 }]; var biggestNumber = 0; for (var i = 0; i < array.length; i++) { var item = array[i]; if (biggestNumber < item.Price) { biggestNumber = item.Price; } } console.log(biggestNumber);
Note:
If you want the whole item
as result, you just need to compare the two objects Price
property, update your first code like this:
for (var i = 0; i < array.length; i++){
var item = array[i]
if(biggestNumber.Price < item.Price) {
biggestNumber = item;
}
}
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.