如何使用javascript获取价格最高的对象:
我有这段代码,但效果不好
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);
答案 0 :(得分:3)
let highestPrice = Math.max.apply(null, array.map(e => e.Price));
let item = array.find(e => e.Price === highestPrice);
答案 1 :(得分:2)
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);
&#13;
答案 2 :(得分:1)
您可以使用Array.prototype.reduce
来减少&#34;将整个数组转换为单个值:价格最高的对象。
var itemWithHighestPrice = array.reduce(function(highest, item, index) {
if (index === 0 || item.Price > highest.Price) {
return item;
}
return highest;
}, {});
答案 3 :(得分:1)
你必须将item.Price存储在largestNumber而不是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);
答案 4 :(得分:1)
使用for
statement的经典方法。
此提议将第一个元素作为临时结果,并使用结果检查数组的所有后续对象。如果找到更大的Price
,则result
将更改为实际元素。
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);
或者您可以使用Array#reduce
并在回调中返回检查结果。
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);
与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);
答案 5 :(得分:1)
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);
答案 6 :(得分:1)
<强>问题:强>
您的问题是,您要将整个item
对象存储在highestPrice
变量中,然后尝试将Number
与object
进行比较。
<强>解决方案:强>
您应该只在item.Price
变量中存储highestPrice
并比较Price
值,然后像这样编辑代码:
var biggestNumber = 0;
for (var i = 0; i < array.length; i++) {
var item = array[i];
if (biggestNumber < item.Price) {
biggestNumber = item.Price;
}
}
工作代码段
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);
&#13;
注意:强>
如果你想要整个item
作为结果,你只需要比较两个对象Price
属性,更新你的第一个代码:
for (var i = 0; i < array.length; i++){
var item = array[i]
if(biggestNumber.Price < item.Price) {
biggestNumber = item;
}
}