如何在 jquery 数组集合中获取值的范围

How to get range of value in jquery array collection

本文关键字:获取 范围 集合 jquery 数组      更新时间:2023-09-26

在这里我使用了数组集合:

var arr = [23, 24, 34, 43, 53, 34];

我想获取 21 到 46 之间的值。 我不想循环或索引到数组值。

有什么功能可以像arr.range(21,46)一样吗? 我怎样才能得到它?

试试

var arr = [23, 24, 34, 43, 53, 34]; //your input array
var min = 21; //your min value
var max = 46; //your max value
//check each item `value` in the array one by one
var newArray = arr.filter(function(value) {
  //if value falls in min and max then return true else false
  return (value >= min && value <= max);
})
console.log(newArray);
<!-- Results pane console output; see http://meta.stackexchange.com/a/242491 -->
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

您可以使用过滤器功能。

function getValues(value) {
  return value >= 21 && value <= 46;
}
var arr = [23, 24,34,43,53,34].filter(getValues);