Does javascript has similar functionality as Ruby has?
array.select {|x| x > 3}
Something like:
array.select(function(x) { i
yo can extend your JS with a select method like this
Array.prototype.select = function(closure){
for(var n = 0; n < this.length; n++) {
if(closure(this[n])){
return this[n];
}
}
return null;
};
now you can use this:
var x = [1,2,3,4];
var a = x.select(function(v) {
return v == 2;
});
console.log(a);
or for objects in a array
var x = [{id: 1, a: true},
{id: 2, a: true},
{id: 3, a: true},
{id: 4, a: true}];
var a = x.select(function(obj) {
return obj.id = 2;
});
console.log(a);