How do I pass an extra parameter to the callback function in Javascript .filter() method?

后端 未结 8 1053
抹茶落季
抹茶落季 2020-12-22 16:31

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

function startsWith(element) {
    return element.indexOf(wordTo         


        
8条回答
  •  甜味超标
    2020-12-22 16:58

    function startsWith(element, wordToCompare) {
        return element.indexOf(wordToCompare) === 0;
    }
    
    // ...
    var word = "SOMETHING";
    
    addressBook.filter(function(element){
        return startsWith(element, word);
    });
    

提交回复
热议问题