简体   繁体   English

用于if循环JavaScript的原型库

[英]Prototype Library for if loop JavaScript

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? prototypejs库中是否有任何函数可以将for循环与if语句组合在一起? 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. 我在想在invoke和each之间进行交叉,这样您就可以遍历数组并对其执行单个方法,如果返回“ true”,则您将执行某项操作,否则请执行任何操作。 It seems like a very common usecase so I would assume something in some js library functions like this. 看来这是一个非常常见的用例,所以我会在这样的js库函数中假设一些东西。

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

Currently I do it like that. 目前我是那样做的。

From the documentation : 从文档中:

Enumerable#findAll Enumerable#findAll

Returns all the elements for which the iterator returned a truthy value. 返回迭代器为其返回真值的所有元素。 For the opposite operation, see Enumerable#reject 对于相反的操作,请参见Enumerable#reject

EXAMPLE

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM