'How to display values from an array of objects within a given range with RamdaJS
I am trying to create a function to filter an array of objects. I need to display all numbers between 2 numbers that I put in my search. I would like to use Ramda. Does anyone have any suggestions?
For example: I have following array of objects:
[{num: 120},{num: 110},{num: 115},{num: 5},{num: 35}]
I would like to display all numbers between 99 and 119. How can I do that with RamdaJS?
Solution 1:[1]
.filter is the function for this. For example:
const inputArray = [{num: 120},{num: 110},{num: 115},{num: 5},{num: 35}];
const min = 99;
const max = 119;
const outputArray = R.filter(val => val >= min && val <= max, inputArray);
If you're trying to have a factory for creating filter functions with different min and max values, that would look something like:
function makeFilter(min, max) {
return R.filter(val => val >= min && val <= max);
}
// used like:
const filter = makeFilter(99, 119);
const outputArray = filter(inputArray);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nicholas Tower |
