EN
JavaScript - equivalent method to Ruby array.select()
1 answers
0 points
Does JavaScript have a similar method to Array select()
from Ruby?
I want something like this:
xxxxxxxxxx
1
array.select {|x| x < 3}
For example:
xxxxxxxxxx
1
const array = [1, 2, 3, 4, 5];
2
3
const result = array.select((x) => {
4
if (x < 3) return true;
5
});
6
7
console.log(result); // Output: 1, 2
1 answer
0 points
In JavaScript, there is filter()
method that does exactly what you need.
Practical example
xxxxxxxxxx
1
const array = [1, 2, 3, 4, 5];
2
3
const result = array.filter((x) => x < 3);
4
5
console.log(result); // [ 1, 2 ]
See also
References
0 commentsShow commentsAdd comment