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 214179
In Process

The Archive Base Latest Questions

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

Is there a way in JavaScript to compare values from one array and see

  • 0

Is there a way in JavaScript to compare values from one array and see if it is in another array?

Similar to PHP’s in_array function?

  • 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-11T18:20:59+00:00Added an answer on May 11, 2026 at 6:20 pm

    No, it doesn’t have one. For this reason most popular libraries come with one in their utility packages. Check out jQuery’s inArray and Prototype’s Array.indexOf for examples.

    jQuery’s implementation of it is as simple as you might expect:

    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(haystack[i] == needle) return true;
        }
        return false;
    }
    

    If you are dealing with a sane amount of array elements the above will do the trick nicely.

    EDIT: Whoops. I didn’t even notice you wanted to see if an array was inside another. According to the PHP documentation this is the expected behavior of PHP’s in_array:

    $a = array(array('p', 'h'), array('p', 'r'), 'o');
    
    if (in_array(array('p', 'h'), $a)) {
        echo "'ph' was found\n";
    }
    
    if (in_array(array('f', 'i'), $a)) {
        echo "'fi' was found\n";
    }
    
    if (in_array('o', $a)) {
        echo "'o' was found\n";
    }
    
    // Output:
    //  'ph' was found
    //  'o' was found
    

    The code posted by Chris and Alex does not follow this behavior. Alex’s is the official version of Prototype’s indexOf, and Chris’s is more like PHP’s array_intersect. This does what you want:

    function arrayCompare(a1, a2) {
        if (a1.length != a2.length) return false;
        var length = a2.length;
        for (var i = 0; i < length; i++) {
            if (a1[i] !== a2[i]) return false;
        }
        return true;
    }
    
    function inArray(needle, haystack) {
        var length = haystack.length;
        for(var i = 0; i < length; i++) {
            if(typeof haystack[i] == 'object') {
                if(arrayCompare(haystack[i], needle)) return true;
            } else {
                if(haystack[i] == needle) return true;
            }
        }
        return false;
    }
    

    And this my test of the above on it:

    var a = [['p','h'],['p','r'],'o'];
    if(inArray(['p','h'], a)) {
        alert('ph was found');
    }
    if(inArray(['f','i'], a)) {
        alert('fi was found');
    }
    if(inArray('o', a)) {
        alert('o was found');
    }  
    // Results:
    //   alerts 'ph' was found
    //   alerts 'o' was found
    

    Note that I intentionally did not extend the Array prototype as it is generally a bad idea to do so.

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

Sidebar

Ask A Question

Stats

  • Questions 119k
  • Answers 119k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No there is not via reflection. Type definitions cannot be… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer You need to add a DirectoryIndex directive to your apache… May 11, 2026 at 11:47 pm
  • Editorial Team
    Editorial Team added an answer Sorry Mohsan, that's the wrong way. d. showed the right… May 11, 2026 at 11:47 pm

Related Questions

First off: I'm using a rather obscure implementation of javascript embedded as a scripting
I've created a javascript function that allows me to validate if one field or
I am trying to compare the performance of several Javascript libraries. Although measuring transaction
Is there a 3rd party add-on/application or some way to perform object map dumping

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.