Skip to content Skip to sidebar Skip to footer

Looping Through Arrays Within An Array, If Particular Array Has A Specific Word, Grab That Array

I'm trying to create a search function where a search word is passed through (for example, 'Food' or 'Gym') and the function will loop through the arrays in the array, if that word

Solution 1:

Your question is not exactly clear. Is this what you need? It returns the nearest array that contains the matching word. I used RegExp test to find the matching word, if you need to find the exact words, use === instead since test is not as performant. I only put there since you can write some more versatile random RegExp with it.

functionwordFilter(keyword, list) {

  var filter = [];

  recursiveFilter(keyword, list);

  functionrecursiveFilter(keyword, list) {
      //Loop if list is array, otherwise test if it matches the keywordif(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 sureif(Array.isArray(list[i])){
                    filter.push(list[i]);
                } else {
                    filter.push(list);
                }
                break;
            }
        }
      } else {
        var reg = newRegExp(keyword);
        return reg.test(list);
      }
   }
   return filter;
}

Solution 2:

functionwordFilter(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"));

Solution 3:

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

Solution 4:

As a follow up of my comment, here is a sample code using indexOf() instead of match() in order to know if a string contains a substring. I'll also use filter() and join() instead of two for loops.

var findIn = functionfindIn(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));

The conditional part, return el.join(' ').indexOf(needle) > -1;, might be adapted to your needs, using lower case match only, regular expression or any other method that will return a boolean.

Post a Comment for "Looping Through Arrays Within An Array, If Particular Array Has A Specific Word, Grab That Array"