admin管理员组文章数量:1425109
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);
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);
Share
Improve this question
asked Apr 5, 2017 at 14:41
Edin PuzicEdin Puzic
1,0482 gold badges23 silver badges43 bronze badges
4
- I would suggest you to either use Array.reduce or Array.sort to acplish such. Both examples: jsfiddle/63L9z07a . Reduce is more efficient. – briosheje Commented Apr 5, 2017 at 14:42
-
3
if (biggestNumber.Price < item.Price) {...
You're storing the whole object, not just the number. And initializebiggestNumber
with an object that has aPrice
. – user1106925 Commented Apr 5, 2017 at 14:43 - 1 If this user is having difficulty with a traditional approach, do these answerers using built in functional constructs and no explanation think their solution will be understood? Are we just showing off? – user1106925 Commented Apr 5, 2017 at 14:54
- 1 Well pointed out @squint. – cнŝdk Commented Apr 5, 2017 at 15:01
7 Answers
Reset to default 3let 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);
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);
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);
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 pare a Number
with an object
.
Solution:
You should store only the item.Price
in your highestPrice
variable and pare 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 pare 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;
}
}
本文标签: javascriptGet object from array with largest priceStack Overflow
版权声明:本文标题:javascript - Get object from array with largest price - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745443037a2658533.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论