最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get object from array with largest price - Stack Overflow

programmeradmin0浏览0评论

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 initialize biggestNumber with an object that has a Price. – 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
Add a ment  | 

7 Answers 7

Reset to default 3
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);

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;
  }
}
发布评论

评论列表(0)

  1. 暂无评论