简体   繁体   English

返回大于数字的数组元素

[英]Return array elements larger than a number

I have the following function: 我有以下功能:

function isBigEnough(element, index, array) {
  return (element >= 10);
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough);
// filtered is [12, 130, 44] 

How can I return values that are larger than (or equal to) a number other than 10 ? 如何返回大于(或等于) 10以外的数字的值? For example, array.filter(isBigEnough(15)) would give me 44, 130 例如, array.filter(isBigEnough(15))会给我44, 130

Functions are first-class citizens in JS, so you can create a function which returns another function: 函数是JS中的一等公民,因此您可以创建一个返回另一个函数的函数:

function isBigEnough(value) {
  return function(element, index, array) {
    return (element >= value);
  }
}
var filtered = [12, 5, 8, 130, 44].filter(isBigEnough(10));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM