简体   繁体   English

尝试按搜索词搜索数组时忽略过滤器 function

[英]filter function is ignored when attempting to search an array by a search term

I'm attempting to write a similar function to this to filter a large array based on a search term:我正在尝试为此编写类似的 function 以根据搜索词过滤大型数组:

function my_func(my_list, term) {
    my_list.filter(function(card){
    if(card.includes(term)){
        return card;
    }
    else {
      return  "something else"
    }
    })
  return "inside"
}

const my_list = ["merloc", "spider", "orc",  "wisp", "giant"]

my_func(my_list, "wisp")

However card is never returned.但是card永远不会退还。 In fact not even "something else" is returned - the only result is "inside".事实上,甚至没有返回“其他东西”——唯一的结果是“内部”。 How can I get the above function to return card based on the search term?如何根据搜索词得到上述function回card

A small fix add return before my_list.filter一个小修复在 my_list.filter 之前添加返回

function my_func(my_list, term) {
  return my_list.filter(function(card){
    if(card.includes(term)){
       return card;
    }
    else {
       return  "something else"
    }
 })
 return "inside"
}

const my_list = ["merloc", "spider", "orc",  "wisp", "giant"]

my_func(my_list, "wisp")

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

相关问题 使用搜索词过滤json数组 - Use a search term to filter through a json Array AngularJS 过滤器对象数组与 JavaScript 中的搜索词 - AngularJS filter object array with search term in JavaScript 使用搜索词过滤对象数组会得到“TypeError:includes is not a function” - Filtering array of objects with a search term gets “TypeError: includes is not a function” A Javascript function 用搜索词过滤树结构 json。 排除与搜索词不匹配的任何 object - A Javascript function to filter tree structured json with a search term. exclude any object which donot match the search term 随处包含搜索词的过滤元素 - Filter elements containing search term anywhere 通过将嵌套属性与搜索词进行比较来过滤 - Filter by comparing a nested property to a search term 在backgrid.js过滤器中预设搜索词 - Preset search term in backgrid.js filter Javascript 产品搜索(工作,但需要按搜索词过滤) - Javascript Product Search (working, but need to filter by search term) 在数组中搜索术语并返回包含该术语的数组条目 - Search for term in array and return array entry containing that term 在任何指定键值中包含搜索词的对象数组中过滤特定键 - Filter specific key(s) in array of objects that contain search term in any specified key value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM