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

时间:2016-02-17 11:23:44

标签: javascript jquery arrays

这里我使用了数组集合:

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

我希望得到21到46之间的值。我不想循环或索引到数组值。

是否有任何功能可以像arr.range(21,46)一样?我怎么能得到它?

2 个答案:

答案 0 :(得分:3)

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>

答案 1 :(得分:1)

您可以使用过滤器功能。

"/