如何在 Javascript .filter() 方法中将额外的参数传递给回调函数? [英] How do I pass an extra parameter to the callback function in Javascript .filter() method?

查看:26
本文介绍了如何在 Javascript .filter() 方法中将额外的参数传递给回调函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将数组中的每个字符串与给定的字符串进行比较.我目前的实现是:

function startsWith(element) {返回 element.indexOf(wordToCompare) === 0;}addressBook.filter(startsWith);

这个简单的函数有效,但只是因为现在 wordToCompare 被设置为全局变量,但当然我想避免这种情况并将其作为参数传递.我的问题是我不确定如何定义 startsWith() 所以它接受一个额外的参数,因为我真的不明白它采用的默认参数是如何传递的.我尝试了所有我能想到的不同方法,但都没有奏效.

如果您还可以解释传递给内置"回调函数的参数(抱歉,我不知道更好的术语)如何工作,那就太好了

解决方案

使 startsWith 接受要比较的单词并返回一个函数,然后将其用作过滤器/回调函数:

function startsWith(wordToCompare) {返回函数(元素){返回 element.indexOf(wordToCompare) === 0;}}addressBook.filter(startsWith(wordToCompare));

另一种选择是使用 Function.prototype.bind [MDN](仅在支持 ECMAScript 5 的浏览器中可用,请点击旧浏览器的垫片链接)并修复"第一个论点:

function startsWith(wordToCompare, element) {返回 element.indexOf(wordToCompare) === 0;}addressBook.filter(startsWith.bind(this, wordToCompare));

<小时><块引用>

我不太明白它所带的默认参数是如何传递的

没有什么特别之处.在某些时候,filter 只是调用回调并传递数组的当前元素.所以它是一个调用另一个函数的函数,在这种情况下是你作为参数传递的回调.

这是一个类似函数的例子:

function filter(array, callback) {var 结果 = [];for(var i = 0, l = array.length; i < l; i++) {if(callback(array[i])) {//这里用当前元素调用回调result.push(array[i]);}}返回结果;}

I want to compare each string in an Array with a given string. My current implementation is:

function startsWith(element) {
    return element.indexOf(wordToCompare) === 0;
}
addressBook.filter(startsWith);

This simple function works, but only because right now wordToCompare is being set as a global variable, but of course I want to avoid this and pass it as a parameter. My problem is that I am not sure how to define startsWith() so it accepts one extra parameter, because I dont really understand how the default parameters it takes are passed. I've tried all the different ways I can think of and none of them work.

If you could also explain how the passed parameters to 'built in' callback functions (sorry, I dont know of a better term for these) work that would be great

解决方案

Make startsWith accept the word to compare against and return a function which will then be used as filter/callback function:

function startsWith(wordToCompare) {
    return function(element) {
        return element.indexOf(wordToCompare) === 0;
    }
}

addressBook.filter(startsWith(wordToCompare));

Another option would be to use Function.prototype.bind [MDN] (only available in browser supporting ECMAScript 5, follow a link for a shim for older browsers) and "fix" the first argument:

function startsWith(wordToCompare, element) {
    return element.indexOf(wordToCompare) === 0;
}

addressBook.filter(startsWith.bind(this, wordToCompare));


I dont really understand how the default parameters it takes are passed

There is nothing special about it. At some point, filter just calls the callback and passes the current element of the array. So it's a function calling another function, in this case the callback you pass as argument.

Here is an example of a similar function:

function filter(array, callback) {
    var result = [];
    for(var i = 0, l = array.length; i < l; i++) {
        if(callback(array[i])) {  // here callback is called with the current element
            result.push(array[i]);
        }
    }
    return result;
}

这篇关于如何在 Javascript .filter() 方法中将额外的参数传递给回调函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆