简体   繁体   English

将循环重写为filter()javascript

[英]rewrite for loop as filter() javascript

I have the following JavaScript code: 我有以下JavaScript代码:

var selectEnabledGenerators = function(generators) {
    //List of generator indexes to show
    var list = generators;
    var allGenerators = $(".generatorContainer");
    //Hide all generators
    allGenerators.hide();
    //maybe use filter here?
    for (var i = 0, max = list.length; i < max; i++) {
        $(".generatorContainer[data-generator=" + list[i] + "]").show();
    }
};

Is there any way to rewrite the for loop using filter() ? 有什么方法可以使用filter()重写for循环吗? I know that I can use a for each loop but I want to do this by using filter. 我知道我可以for each循环使用a for each但是我想通过使用filter来做到这一点。

There's no need to use filter here. 此处无需使用filter Since you're not returning anything forEach would be the more appropriate function. 由于您不为forEach返回任何内容,因此它是更合适的功能。

Something along the lines of this should do: 应该遵循以下步骤:

list.forEach(item => $(".generatorContainer[data-generator=" + item + "]").show());

or 要么

list.forEach(function(item){
    $(".generatorContainer[data-generator=" + item + "]").show();
});

if you don't like/can't use lambdas 如果您不喜欢/不能使用lambdas


Can I use filter anyway 我还能使用过滤器吗

Yes. 是。 But there's really no reason to. 但是,实际上没有理由这么做。 If I saw you using filter in this way I would reject your code review. 如果我看到您以这种方式使用过滤器,我将拒绝您的代码审查。

The use case of filter is to quickly pare down a list of items by passing in a predicate function (a function that answers "does this stay in"). 过滤器的用例是通过传入谓词函数(一个回答“是否保留”的函数)来快速缩减项目列表。 This function's type for an Array<T> would be T => boolean . Array<T>此函数的类型为T => boolean This filter function will execute the predicate function's code block on every item and then check the return value of that predicate function. 此过滤器函数将在每个项目上执行谓词函数的代码块,然后检查该谓词函数的返回值。 If that return value is truthy, it will mark that object that was passed into the predicate function and then return all the objects that resulted in truthy values as a new array. 如果该返回值是真实的,它将标记传递给谓词函数的对象,然后将所有导致产生真实值的对象作为新数组返回。 forEach will also execute a function on each parameter, just without doing the extra work of returning a value and managing a new list. forEach还将对每个参数执行一个函数,而无需执行返回值和管理新列表的额外工作。

If you do not make use of the returned result from filter, it is nonsensical to use filter. 如果您不使用过滤器返回的结果,则使用过滤器是没有意义的。 Not only is it useless, it will confuse people reading your code in the future who are trying to understand why you use filter here. 它不仅没有用,而且还会使将来试图阅读您的代码并试图理解为什么在这里使用过滤器的人们感到困惑。

Ultimately the code is the same: 最终,代码是相同的:

list.filter(item => $(".generatorContainer[data-generator=" + item + "]").show());

The .show() is treated as a side effect (which filter functions really should not have ). .show()被视为副作用(过滤器功能实际上不应该具有 )。

You could use .filter() like this: 您可以这样使用.filter()

allGenerators.filter(function () {
    return list.indexOf(+$(this).attr('data-generator')) > -1
}).show();

Simplified demo: 简化的演示:

 $('div').hide().filter(function (i) { return [1,3].indexOf(+$(this).attr('data-generator')) > -1 }).show(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div data-generator=1>test1</div> <div data-generator=2>test2</div> <div data-generator=3>test3</div> <div data-generator=4>test4</div> 

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

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