我有以下数据
shots = [
{id: 1, amount: 2},
{id: 2, amount: 4}
]
现在我正在尝试获取具有最高数量的对象。 我已经尝试使用reduce,如下所示。
let highest = shots.reduce((max, shot) => {
return shot.amount > max ? shot : max, 0
});
但我总是得到最低分,你有什么想法可能是我错过了什么吗?
谢谢。
我有以下数据
shots = [
{id: 1, amount: 2},
{id: 2, amount: 4}
]
let highest = shots.reduce((max, shot) => {
return shot.amount > max ? shot : max, 0
});
但我总是得到最低分,你有什么想法可能是我错过了什么吗?
谢谢。
简洁的2行解决方案 :)
const amounts = shots.map((a) => a.amount)
const highestAmount = Math.max(...amounts);
更新
以上代码将返回最高金额。如果你想获取包含它的对象,你将面临许多对象都包含最高值的可能性。因此,你需要使用 filter
。
const highestShots = shots.filter(shot => shot.amount === highestAmount)
const highest = shots.find(e => e.amount === highestAmount)
- scagoodvar highestShots = shots.filter(shot => shot.amount === highestAmount)
就可以了。 - Cristian S.这里有两个问题,第一个是reduce需要返回值,第二个是你正在将一个数字与一个对象进行比较。
因此,我认为你需要像这样做:
// This will return the object with the highest amount.
let highest = shots.reduce((max, shot) => {
return shot.amount >= max.amount ? shot : max;
}, {
// The assumption here is that no amount is lower than a Double-precision float can go.
amount: Number.MIN_SAFE_INTEGER
});
// So, we can convert the object to the amount like so:
highest = highest.amount;
一个简洁的一行代码会像这样:
const highest = shots.sort((a, b) => b.amount - a.amount)[0]
-4 > undefined
是错误的,而在这种情况下,-4 将是最大的数字。因此,这不是最好的答案。同时,您还损失了 id
属性。 - Rogier Slag-4> 0
=> false
,因此如果数组中唯一的条目具有金额-4
,则您的代码将返回0
。 - Rogier Slag请尝试以下方法:
更新
let highest = shots.reduce((max, shot) => {
return shot.amount > max.amount ? shot : max
}, {amount:0});
很遗憾,这里的大多数答案都没有正确考虑到所有情况。
为了正确处理负值,您需要使用 -Infinity
作为种子值。
其次,将该点的最大值与新值进行比较。
您会得到以下结果:
highest = shots.reduce((max, current) => current.amount >= max.amount ? current : max, {amount: -Infinity})
shots = [
{id: 1, amount: -2},
{id: 2, amount: -4},
{id: 3, amount: -4},
{id: 4, amount: -5},
]
highest = shots.reduce((max, current) => current.amount >= max.amount ? current : max, {amount: -Infinity}) // Returns id 1, amount -2
shots = [
{id: 1, amount: 2},
{id: 2, amount: 4},
{id: 3, amount: 4},
{id: 4, amount: 5},
]
highest = shots.reduce((max, current) => current.amount > max.amount ? current : max, {amount: -Infinity}) // Returns id 4 amount 5
{amount: -Infinity}
,因此您可能需要在执行reduce之前处理shots.length === 0
的情况。let highest = shots.reduce((a, b) => a.amount > b.amount ? a : b);
let highest = shots.reduce((r, o) => {
if (!r || o.amount > r[0].amount) return [o];
if (o.amount === r[0].amount) r.push(o);
return r;
}, undefined);
Math.max.apply(Math,shots.map((shot)=>{return shot.amount;}));
有几个错误
shot.amount
而不是 shot
最后
shots.reduce((max, shot) =>
shot.amount > max ? shot.amount : max, 0);
演示
var shots = [
{id: 1, amount: 2},
{id: 2, amount: 4}
];
var output = shots.reduce((max, shot) =>
shot.amount > max ? shot.amount : max, 0);
console.log( output );
编辑
如果需要返回整个对象,则初始化器应该是一个带有amount
属性的对象。
var shots = [
{id: 1, amount: 2},
{id: 2, amount: 4} ];
var output = shots.reduce((max, shot) =>
shot.amount > max.amount ? shot : max , {amount:0});
console.log( output );
shots.reduce((max, shot) => shot.amount > max.amount ? shot : max)
- CD..