Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I filter an array based on a range of numbers?

I have an array that I filter with this function:

function filter(arr, criteria) {
    return arr.filter(function(obj) {
        return Object.keys(criteria).every(function(c) {
            return !(criteria[c]) || obj[c] == criteria[c];
        });
    });
}

var arr = filter(arr, { dep: dv, arr: av, car: cv, epi: acv, dur: dv }); 

And I have a bunch of options that the user can choose from in the select. For duration, here is what I have:

<select name="duration" id="duration">
  <option selected disabled hidden value="">-</option>
  <option value="l1">Less than 1 hour</option>
  <option value="1to3">1 to 3 hours</option>
  <option value="3to6">3 to 6 hours</option>
  <option value="6to10">6 to 10 hours</option>
  <option value="m10">More than 10 hours</option>
</select>

But that filter is based on exact criteria. I want to filter float numbers in arr that are between 1 and 3 or 6 and 10. I also want to be able to run my other filters that you see up there with dep, arr, car, epi, dur. Is there any way I can do this?

like image 819
ChrisRockGM Avatar asked May 30 '14 02:05

ChrisRockGM


People also ask

How do I use the filter () method of the array object?

The filter () method accepts two named arguments: a callback function and an optional object. Like other iterative methods of the Array object such as every (), some (), map () and forEach (), the callback function has the following form: function callback(currentElement, index, array) { //...

How many times is the filter function called in an array?

The filter function is called once per element in the array. You are not doing a comparison of any of the elements in the function in your post. Show activity on this post. Show activity on this post. Show activity on this post. A more flexible filtering array from another array which contain object properties

How do you filter an array with callback function?

Internally, the filter () method iterates over each element of the array and pass each element to the callback function. If the callback function returns true, it includes the element in the return array. The filter () method accepts two named arguments: a callback function and an optional object.

What are the index and array arguments of the filter () method?

The index and array arguments are optional. The contexObject argument of the filter () method is optional. If you pass the this value, you canreference it by using this keyword inside the callback function. It is important to note that the filter () method does not change the original array.


1 Answers

Use array.filter with a callback. The code below uses a dictionary with entries corresponding to option values.

var myArray = [1,2,3,3.1,Math.PI,4,4.3,6.1]; //sample array
//Dictionary from option values to functions (JS objects work like hashtables)
var options = {
  "l1":   function(a){return a.duration<1;},
  "1to3": function(a){return a.duration>=1 && a.duration<3;},
  "3to6": function(a){return a.duration>=3 && a.duration<=6;},
...
//When you need to filter the array, do it like so:
var myNewArray = myArray.filter(
  options[ document.getElementById('duration').selectedOptions[0].value ]
);
like image 188
Zaq Avatar answered Sep 29 '22 18:09

Zaq