循环遍历数组中的数组,如果特定数组具有特定单词,则抓取该数组

时间:2017-05-09 06:52:14

标签: javascript arrays loops

我正在尝试创建一个搜索单词,其中搜索单词通过(例如,“Food”或“Gym”),并且该函数将循环遍历数组中的数组,如果在单词中找到该单词嵌套数组,它将精确数组推送到过滤数组中。然后我可以向用户显示。

我尝试过寻找不同的解决方案,但没有运气。这是我到目前为止所做的。

 // Filter list with search keyword
    function filter(keyword,list) {
      // placeholder for matches
      var filtered = [];
      // loop through list
      for (var i = 0; i < list.length; i++) {
        var store = list[i];

        // loop through each individual array/store in list
        for (var y = 0; y < store.length; y++) {
        // if match keyword
        if(store[y].match(keyword)){
          // push that store into filtered array
                filtered.push(store);
          }

      else{
        alert('cannot find');
      };    
     };
  };    
};     



var list=[
    ["Mikes Gym",
    "21 Rosenberg Road",
    "Heidelberg",
    "Come get your ice",
    "5",
    ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"],
    "9104-1059",
    "Gym"
    ],
      ["Mikes Cafe",
    "21 Rosenberg Road",
    "Heidelberg",
    "Come get your ice",
    "5",
    ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"],
    "9104-1059",
     "Food"
    ],
      ["Mikes Hairdresser",
    "21 Rosenberg Road",
    "Heidelberg",
    "Come get your ice",
    "5",
    ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"],
    "9104-1059",
     "Hairdresser"
    ],
      ["Mikes Nightclub",
    "21 Rosenberg Road",
    "Heidelberg",
    "Come get your ice",
    "5",
    ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"],
    "9104-1059",
     "Club"
    ],
      ["Mikes Groceries",
    "21 Rosenberg Road",
    "Heidelberg",
    "Come get your ice",
    "5",
    ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"],
    "9104-1059",
     "Shop"
    ]
  ];

filter("Food",list)

4 个答案:

答案 0 :(得分:0)

你的问题并不完全清楚。这是你需要的吗?它返回包含匹配单词的最近数组。 我使用RegExp测试找到匹配的单词,如果你需要找到确切的单词,请使用===代替,因为测试不是那么高效。我只是放在那里,因为你可以用它写一些更通用的随机RegExp。

function wordFilter(keyword, list) {

  var filter = [];

  recursiveFilter(keyword, list);

  function recursiveFilter(keyword, list) {
      //Loop if list is array, otherwise test if it matches the keyword
      if(Array.isArray(list)) {

        for(var i = 0; i < list.length; i++) {

            if(recursiveFilter(keyword, list[i])) {
                //This unlikely to return true, I just put it there to make sure
                if(Array.isArray(list[i])){
                    filter.push(list[i]);
                } else {
                    filter.push(list);
                }
                break;
            }
        }
      } else {
        var reg = new RegExp(keyword);
        return reg.test(list);
      }
   }
   return filter;
}

答案 1 :(得分:0)

function wordFilter(list, keyword) {
    var returnVal;
    list.forEach(function(childList) {
        childList.foreach(function(value) {
            if (value === keyword) {
                returnVal = childList;
            }
        })
    });
    return returnVal;
}

console.log(wordFilter([["test", "monkeys"], ["car", "airplane"]], "test"));

答案 2 :(得分:0)

     // Filter list with search keyword
    function filter(keyword,list) {
      // placeholder for matches
      var filtered = [];
      // loop through list
      for (var i = 0; i < list.length; i++) {
      var store=list[i];
        for (var y = 0; y < store.length; y++) {
        // if match keyword
        if(store[y]==keyword){
          // push that store into filtered array
                filtered.push(list[i]);
          }
      } 
    }
   return filtered;
    }    `

答案 3 :(得分:0)

作为对我的评论的跟进,以下是使用indexOf()而不是match()的示例代码,以便了解字符串是否包含子字符串。我还会使用filter()join()而不是两个for循环。

var findIn = function findIn(needle, haystack) {
        return haystack.filter(function(el) {
            return el.join(' ').indexOf(needle) > -1;
        });
    }
  , list = ["Mikes Gym", "21 Rosenberg Road", "Heidelberg", "Come get your ice", "5", ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"], "9104-1059", "Gym" ], ["Mikes Cafe", "21 Rosenberg Road", "Heidelberg", "Come get your ice", "5", ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"], "9104-1059", "Food" ], ["Mikes Hairdresser", "21 Rosenberg Road", "Heidelberg", "Come get your ice", "5", ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"], "9104-1059", "Hairdresser" ], ["Mikes Nightclub", "21 Rosenberg Road", "Heidelberg", "Come get your ice", "5", ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"], "9104-1059", "Club" ], ["Mikes Groceries", "21 Rosenberg Road", "Heidelberg", "Come get your ice", "5", ["onsequat. Phasellus diam .", "malesuada in. Integer eget molestie mi. Etiam a"], "9104-1059", "Shop" ] ]
  ;

console.log(findIn("Food",list));

条件部分return el.join(' ').indexOf(needle) > -1;可能会根据您的需要进行调整,仅使用小写匹配,正则表达式或任何其他将返回布尔值的方法。