ES6查找对象数组的最大数量

时间:2018-02-14 12:06:11

标签: javascript ecmascript-6

我有以下数据

shots = [
    {id: 1, amount: 2},
    {id: 2, amount: 4}
]
  

现在我试图获得金额最高的对象

我尝试过使用reduce,如下所示

let highest = shots.reduce((max, shot) => {
    return shot.amount > max ? shot : max, 0
});

但我总是得到最低的数字。 知道我可能会缺少什么吗?

谢谢。

7 个答案:

答案 0 :(得分:6)

清洁2行解决方案:)

const amounts = shots.map((a) => a.amount)
const highestAmount = Math.max(...amounts);

<强>更新

以上代码将返回最高金额。如果要获取包含它的对象,您将面临许多对象包含最高值的可能性。所以你需要filter

const highestShots = shots.filter(shot => shot.amount === highestAmount)

答案 1 :(得分:5)

这里有两个问题,第一个是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]

答案 2 :(得分:1)

试试这个:

<强>更新

let highest = shots.reduce((max, shot) => {
  return shot.amount > max.amount ? shot : max
}, {amount:0});

答案 3 :(得分:1)

您可以检查两个项目的金额属性并返回最大金额的项目。

let highest = shots.reduce((a, b) => a.amount > b.amount ? a : b);

对于相同数量,您只能获得具有相同金额的第一个对象。

它不适用于空数组。

如果您喜欢具有相同最高amount的所有对象,则可以使用两个条件来减少数组。

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);

答案 4 :(得分:1)

可悲的是,这里的大多数答案都没有正确考虑所有情况

  1. 种子值已关闭
  2. 比较是在不同类型之间进行的
  3. 要正确处理负值,您必须使用-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的情况

答案 5 :(得分:0)

有几个错误

  • 返回shot.amount而不是shot
  • 只需在比较后返回

最后

shots.reduce((max, shot) => 
    shot.amount > max ? shot.amount : max, 0);

<强>演示

&#13;
&#13;
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 );
&#13;
&#13;
&#13;

修改

如果必须返回整个对象,那么初始值设定项应该是具有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 );

答案 6 :(得分:0)

您可以使用此代码。它只会返回最高金额。

Math.max.apply(Math,shots.map((shot)=>{return shot.amount;}));