获取JS中用于过滤的对象数组的最大值

时间:2021-05-20 22:57:37

I have an array of objects like:

我有一个对象数组,如:

var myArr = [{
    number: 5,
    shouldBeCounted: true
}, {
    number: 6,
    shouldBeCounted: true
}, {
    number: 7,
    shouldBeCounted: false
}, ...];

How to find max number for objects with shouldBeCounted set to true? I don't want to use loops, just wondering if this is possible with Math.max.apply (or something like this).

如何找到shouldBeCounted设置为true的对象的最大数量?我不想使用循环,只是想知道这是否可以使用Math.max.apply(或类似的东西)。

4 个解决方案

#1


5  

No it's not possible. You can use Math.max with .map like so

不,这是不可能的。你可以像这样使用Math.max和.map

var myArr = [{
    number: 5,
    shouldBeCounted: true
}, {
    number: 6,
    shouldBeCounted: true
}, {
    number: 7,
    shouldBeCounted: false
}];


var max = Math.max.apply(Math, myArr.map(function (el) {
    if (el.shouldBeCounted) {
        return el.number;
    }
    
    return -Infinity;
}));

console.log(max);

#2


2  

With a simple .reduce():

使用简单的.reduce():

var myArr = [{
  number: 5,
  shouldBeCounted: true
}, {
  number: 6,
  shouldBeCounted: true
}, {
  number: 7,
  shouldBeCounted: false
}];

var max = myArr.reduce(function(max, current) {
  return current.shouldBeCounted ? Math.max(max, current.number) : max;
}, -Infinity);

console.log(max);

Where

哪里

  • myArr.reduce() - Reduces an array to a single value. Accepts a function with two parameters, the current cumulative value, and the current item (also two more optional parameters for the index of the item, and the original array).
  • myArr.reduce() - 将数组减少为单个值。接受具有两个参数的函数,当前累积值和当前项(还有两个可选参数,用于项的索引和原始数组)。
  • return current.shouldBeCounted ? Math.max(max, current.number) : max; - For each item, returns the current max is shouldBeCounted is false, or the max between the current known max and the current number.
  • return current.shouldBeCounted? Math.max(max,current.number):max; - 对于每个项目,返回当前最大值isBeCounted为false,或当前已知最大值与当前数量之间的最大值。
  • , -Infinit - Starting with -Infinity.
  • ,-Infinit - 以-Infinity开头。

The advantage of this approach over the one in the accepted answer is that this will only iterate the array once, while .filter() and .map() loop over the array once each.

这种方法优于接受答案的方法的优点是,这只会迭代数组一次,而.filter()和.map()遍历数组一次。

#3


1  

Another less verbose solution (if your numbers are all positive):

另一个不那么冗长的解决方案(如果你的数字都是正数):

var max = Math.max.apply(Math, myArr.map(function(el) {
    return el.number*el.shouldBeCounted;
}));

#4


0  

You can't. All solutions require walk the array unless you will be storing max number in a variable before adding to the array.

你不能。除非在添加到数组之前将最大数量存储在变量中,否则所有解决方案都需要遍历数组。

#1


5  

No it's not possible. You can use Math.max with .map like so

不,这是不可能的。你可以像这样使用Math.max和.map

var myArr = [{
    number: 5,
    shouldBeCounted: true
}, {
    number: 6,
    shouldBeCounted: true
}, {
    number: 7,
    shouldBeCounted: false
}];


var max = Math.max.apply(Math, myArr.map(function (el) {
    if (el.shouldBeCounted) {
        return el.number;
    }
    
    return -Infinity;
}));

console.log(max);

#2


2  

With a simple .reduce():

使用简单的.reduce():

var myArr = [{
  number: 5,
  shouldBeCounted: true
}, {
  number: 6,
  shouldBeCounted: true
}, {
  number: 7,
  shouldBeCounted: false
}];

var max = myArr.reduce(function(max, current) {
  return current.shouldBeCounted ? Math.max(max, current.number) : max;
}, -Infinity);

console.log(max);

Where

哪里

  • myArr.reduce() - Reduces an array to a single value. Accepts a function with two parameters, the current cumulative value, and the current item (also two more optional parameters for the index of the item, and the original array).
  • myArr.reduce() - 将数组减少为单个值。接受具有两个参数的函数,当前累积值和当前项(还有两个可选参数,用于项的索引和原始数组)。
  • return current.shouldBeCounted ? Math.max(max, current.number) : max; - For each item, returns the current max is shouldBeCounted is false, or the max between the current known max and the current number.
  • return current.shouldBeCounted? Math.max(max,current.number):max; - 对于每个项目,返回当前最大值isBeCounted为false,或当前已知最大值与当前数量之间的最大值。
  • , -Infinit - Starting with -Infinity.
  • ,-Infinit - 以-Infinity开头。

The advantage of this approach over the one in the accepted answer is that this will only iterate the array once, while .filter() and .map() loop over the array once each.

这种方法优于接受答案的方法的优点是,这只会迭代数组一次,而.filter()和.map()遍历数组一次。

#3


1  

Another less verbose solution (if your numbers are all positive):

另一个不那么冗长的解决方案(如果你的数字都是正数):

var max = Math.max.apply(Math, myArr.map(function(el) {
    return el.number*el.shouldBeCounted;
}));

#4


0  

You can't. All solutions require walk the array unless you will be storing max number in a variable before adding to the array.

你不能。除非在添加到数组之前将最大数量存储在变量中,否则所有解决方案都需要遍历数组。