31

I have an array with some values. How can I search that array using jquery for a value which is matched or close to it?

var a = ["foo","fool","cool","god"];

If I want to search for oo, then it should return foo, fool, and cool because these strings contain oo.

Yahel
  • 37,023
  • 22
  • 103
  • 153
KillerFish
  • 5,042
  • 16
  • 55
  • 64

8 Answers8

72

Vanilla JS

To search in the array with Vanilla JS I would use the filter() method implemented into the Array prototype.

Note: For very large arrays you might want to consider refactoring those to async/await functions else it might slow down the user interface.

1. Using regular expressions (slower)

This is the most flexible approach as you could search for different patterns. You should be aware that the search term here is not a plain text, thus you have to escape most of non-alphanumeric chars according to the syntax. You should not pass unprocessed user input directly to the function, as it will not work as expected.

let a = ["foo","fool","cool","god"];
var term = 'oo'; // search term (regex pattern)
var search = new RegExp(term , 'i'); // prepare a regex object
let b = a.filter(item => search.test(item));
console.log(b); // ["foo","fool","cool"]

2. Using indexOf (faster)

In this particular case I would rather use indexOf() which is basically an equivalent of LIKE %term% but much faster than using regular expressions when working with large arrays.

It is a common case to do case-insensitive searches so make sure to use toLowerCase() for both the search terms and the array items. Otherwise remove it everywhere from the examples.

let a = ["foo","fool","cool","god"];
let term = 'oo';
let b = a.filter(item => item.toLowerCase().indexOf(term) > -1);
console.log(b); // ["foo","fool","cool"]

ES6 style (ES2015)

const fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];

const filterItems = (needle, heystack) => {
  let query = needle.toLowerCase();
  return heystack.filter(item => item.toLowerCase().indexOf(query) >= 0);
}

console.log(filterItems('ap', fruits)); // ['apple', 'grapes']
console.log(filterItems('ang', fruits)); // ['mango', 'orange']

ES5 style

var fruits = ['apple', 'banana', 'grapes', 'mango', 'orange'];

function filterItems(needle, heystack) {
  var query = needle.toLowerCase();
  return heystack.filter(function(item) {
      return item.toLowerCase().indexOf(query) >= 0;
  })
}

console.log(filterItems('ap', fruits)); // ['apple', 'grapes']
console.log(filterItems('ang', fruits)); // ['mango', 'orange']

This is the outdated answer

To search in the array with jQuery you might use jQuery.grep() or jQuery.map(). Both return new array with filtered elements using a callback function.

The fastest implementation (case insensitive) is using indexOf and toUpperCase in the callback:

var search_term = 'oo'; // your search term as string
var search = search_term.toUpperCase();
var array = jQuery.grep(a, function(value) {
    return value.toUpperCase().indexOf(search) >= 0;
});

If you don't need case insensitive search you can remove both .toUpperCase() to speed it up even further.

More flexible but much slower (good enough for small arrays) is to use regular expression:

var search_term = "oo"; // search term
var search = new RegExp(search_term , "i");
var arr = jQuery.grep(a, function (value) {
    return search.test(value);
});

or

var search_term = "oo"; // search term
var search = new RegExp(search_term , "i");
var arr = jQuery.map(a, function (value) {
    return value.match(search) ? value : null;
});

Regular expressions allow you to make searches much more complex than %value%. However don't use it if you don't need it because it is many times slower.

you should get an array arr with the matched elements

venimus
  • 5,907
  • 2
  • 28
  • 38
  • how to pass variable instead of "oo" on search = /oo/gi; Is that like search = /my_var/gi; ? – HADI Apr 29 '12 at 11:39
  • 3
    should use `search = new RegExp(my_var, "gi");` /oo/gi is a shortcut of `new RegExp("oo","gi");` but be aware that `$.grep` and `$.map` cannot have extra params, so your `my_var` have to be defined in the global scope, which is not a good practice, but don't see other simple way – venimus May 03 '12 at 09:17
  • Thanks for your comment :) i have already found this solution http://www.dreamincode.net/forums/topic/277324-need-help-on-jquery-array-match/ btw thanks again and rated you as Great Comment – HADI May 08 '12 at 09:18
  • 1
    Dear god, never use "gi" for Regex. http://stackoverflow.com/questions/1520800/why-regexp-with-global-flag-in-javascript-give-wrong-results – marked-down Feb 27 '15 at 09:34
3
function find(arr) {
    var result = [];

    for (var i in arr) {
        if (arr[i].match(/oo/)) {
            result.push(arr[i]);
        }
    }

    return result;
}

window.onload = function() {
    console.log(find(['foo', 'fool', 'cool', 'god']));
};

It prints ["foo", "fool", "cool"]

Rendicahya
  • 4,205
  • 7
  • 36
  • 56
2

Try the following js code

function likeMatch(q)
{
    my_arr = ["foo","fool","cool","god"];
    var rArr=[];

    for(var t in my_arr)
    {
      if(my_arr[t].indexOf(q)>0)   
      rArr.push(my_arr[t]);
    }
  return(rArr);
}
Clyde Lobo
  • 9,126
  • 7
  • 34
  • 61
1

try this:

var infoData = ["foo","fool","cool","god"], 
search   = 'oo';

//this makes the magic
infoData = $$(infoData).filter(function(){
        return (this.search(search) >= 0)
    })  

var n = infoData.length;
    console.log("size result: "+ n );

for(var item = 0; item < n ;item++){
    console.log("item: "+item+" data : "+infoData[item]);       
}

result:

size result: 3

item: 0 data : foo
item: 1 data : fool
item: 2 data : cool 
Ende Neu
  • 15,581
  • 5
  • 57
  • 68
1

You can do it with Alasql JavaScript SQL library. It supports LIKE operator, like in SQL

var a = ["foo","fool","cool","god"];
var searchString = "%oo%";

var res = alasql('SELECT COLUMN * FROM [?] WHERE [0] LIKE ?',[a, searchString]);

Try this example at jfFiddle.

agershun
  • 4,077
  • 38
  • 41
0

use the following function if you are searching in array containing hashes object

 function searchInArrayofHashes(array,key,keyword) {
  responsearr = []
  for(i=0;i<array.length;i++) {
      if(array[i][key].indexOf(keyword) > -1 ) {
        responsearr.push(array[i])
    }
  }
  return responsearr
}
Apoorv
  • 1,338
  • 1
  • 17
  • 18
0

try $.inArray http://api.jquery.com/jQuery.inArray/. I'm not sure if it will allow you to use a regular expression but its worth a try

Yahel
  • 37,023
  • 22
  • 103
  • 153
Alistair Laing
  • 983
  • 1
  • 7
  • 18
0

You can use find method on array if you want to match any regex :

Array.find(x => /^left:/.test(x)
Ajeet Malviya
  • 784
  • 1
  • 5
  • 9