1

I have a rather simple question that I am pretty sure I know the answer to. Are there any functions in the prototypejs library that allow you to combine a for loop with an if statement? I was thinking a cross between invoke and each so that you would iterate through an array and do single method on it, if that returns "true" then you do something, if not you don't do anything. It seems like a very common usecase so I would assume something in some js library functions like this.

var array = {Object, Object, Object}
array.each(function(item) {
  if(item.isTrue())
    doSomething();
});

Currently I do it like that.

4

1 回答 1

0

从文档中:

可枚举#findAll

返回迭代器为其返回真值的所有元素。对于相反的操作,请参阅Enumerable#reject

例子

var arr = ['1', '', '0', 'null', null];
arr.findAll(function(k) {
    return !!k;
}).each(function(f) {
    console.log('i was filtered by findAll and am truthy : ' + f);
});
于 2012-06-26T12:35:21.227 回答