Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4081472
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:03:55+00:00 2026-05-20T18:03:55+00:00

I have an array with some values. How can I search that array using

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T18:03:56+00:00Added an answer on May 20, 2026 at 6:03 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have php code that puts some values into an Array as follows: $hunter=addslashes($MessageArray[1]);
I have some code that produces undefined values in an array making no sense.
Following situation: I have an array with some constant values, which represent ranges. A
I have an array in which I have some values like $errors[]=Invalid username; $errors[]=Invalid
I have a 1D numpy array, and some offset/length values. I would like to
I have an array with some value like this: [Organization_id] => Array ( [0]
Say I have a hashmap, $hash = array('fox' => 'some value', 'fort' => 'some
I have an Array that contains some elements multiple times. Now I want to
I have an array controller holding some values, say - subjects. I am displaying
I have a search form regarding property search having some elements on which search

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.