javascript includes() case insensitive

前端 未结 6 1417
臣服心动
臣服心动 2020-12-14 05:07

lets say I have an array of filter strings that I need to loop and check against with other passed in string.

var filterstrings = [\'firststring\',\'secondst         


        
6条回答
  •  离开以前
    2020-12-14 06:04

    ES6 array method filter() can simply the solution in single line, use includes() method to determines whether an array includes a certain value among its entries.

    var filterstrings = ['firststring','secondstring','thridstring'];
    var passedinstring = localStorage.getItem("passedinstring");
    
    // convert each string from filterstrings and passedinstring to lowercase
    // to avoid case sensitive issue.
    filteredStrings = filterstrings.filter((str) => str.toLowerCase().includes(passedinstring.totoLowerCase())
    

提交回复
热议问题