Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Sep 2012
    Location
    Calgary, Alberta, Canada
    Posts
    29
    Thanks
    4
    Thanked 0 Times in 0 Posts

    How to find a value that exists anywhere in an array

    I am fairly new to javascript, so I hope this explanation makes sense...

    I have an array which I push values to. The array will only have unique values (no duplicates).

    In an example, I have pushed the values 1,2,5,11 into an array.

    In an IF statement, I want to see if "1" exists in the array and if so to perform a function.

    Is there a command in javascript that does this?

    Thank you.

  • #2
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    You could use the Array.some method with a function domparing the array values to the entry you are looking for.

    For example with the array cotaining the values you specified you'd find out if 1 is in the array as follows:

    Code:
    var ary = [1,2,3,11];
    if (ary.some(function(a) return a == 1;)) yourFunction();
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Code:
    <script type = "text/javascript">
    
    var r = [1,2,5,11,19];
    var a = r.indexOf(11); 
    alert (a);
    var a = r.indexOf(29);
    alert (a);
    
    </script>
    Returns the array index, or -1 if not found.

    Note: indexOf is not supported in IE<9. An older method would be

    Code:
    <script type = "text/javascript">
    
    function find(x) {
    var r = [1,2,5,11,19];
    for (var i = 0; i<r.length; i++) {
    if (r[i] == x) {
    alert (x + " was found at index " + i);
    }
    }
    }
    
    find(11);
    
    </script>
    I've got a second bite at the cherry tomorrow, and I am going to take it with both hands. - Interviewee BBC Radio 2
    Last edited by Philip M; 06-23-2014 at 08:16 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Array indexOf polyfill for IE8 and below: https://developer.mozilla.org/en-US/...dexOf#Polyfill
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #5
    New Coder
    Join Date
    Jun 2014
    Posts
    14
    Thanks
    0
    Thanked 2 Times in 2 Posts

    The simplest way

    This is the simplest way to do it.
    var ar=[1,2,3,4,5];for(i in ar){if(ar[i]=="1"){alert("1 found");}}

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by code_generator View Post
    This is the simplest way to do it.
    var ar=[1,2,3,4,5];for(i in ar){if(ar[i]=="1"){alert("1 found");}}
    The "for..in" syntax is probably slower, because the index is a string, not an integer. There is no advantage as far as I can see over a regular for loop.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    [].indexOf(value) is the way to go, polyfill if you want; ie8 is on life-support and won't last much longer now that windows XP has been thrown under the bus...
    with indexOf(), you don't need the words "for" or "function" in your solution, making it the quickest and cleanest way to code it.

    if you polyfill indexOf, you should polyfill all of the handy array methods like [].map, [].filter, [].some, etc.


    you can easily strip out non-unique array values at any time if you need to:
    Code:
    [1,2,3,4,4,3,2,1].filter(function(a,b,c){ return c.indexOf(a)==b;}); // == [1,2,3,4]
    if you need to do this more than once, name the filter function and it becomes almost as handy as an Array.prototype method ([].filter(unq) vs [].unq() ), but without the prototype pollution:

    Code:
    function unq(a,b,c){ return c.indexOf(a)==b;}
    
    [1,2,3,4,4,3,2,1].filter(unq); // == [1,2,3,4]
    
    "hello world".split("").filter(unq); // == ["h", "e", "l", "o", " ", "w", "r", "d"]
    if you're worried about older browsers, and you don't wan't to load 2kb of polyfills, and you only have numbers, you can always use the older and well-established String.prototype.indexOf() instead of the newer Array.prototoype.indexOf():
    Code:
    var r=[1,2,5,11];
    
    (","+r+",").indexOf(","+ 1 +",")!==-1; // true, r contains 1
    (","+r+",").indexOf(","+ 8 +",")!==-1; // false, r doesn't contain 8
    Last edited by rnd me; 06-23-2014 at 10:28 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by rnd me View Post
    [].indexOf(value) is the way to go, polyfill if you want; ie8 is on life-support and won't last much longer now that windows XP has been thrown under the bus...
    with indexOf(), you don't need the words "for" or "function" in your solution, making it the quickest and cleanest way to code it.
    I think you under-estimate how many users of Windows XP are still around.

    http://www.netmarketshare.com/operat...10&qpcustomd=0

    It is Vista which is close to defunct.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by Philip M View Post
    I think you under-estimate how many users of Windows XP are still around.

    Operating system market share

    It is Vista which is close to defunct.
    home users can use firefox or chrome, so XP itself is not a big deal. for a long time, corporate PCs with custom activeX software kept IE8 going strong in the enterprise, but most businesses are dumping XP boxes left and right. i know we're not allowed to use it anywhere at work or even on the VPN anymore. Enterprise was the big chunk that nobody could do anything about because a lot of companies mandated IE8 and nothing else. Without further updates, the cost/benefit analysis of continuing windowXP changes radically, and bosses are finally reacting. Without the corporate prop-up, IE8 will drop even faster than before.

    i'm not saying it will be gone overnight, but most of the world has moved on.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    Quote Originally Posted by rnd me View Post
    i'm not saying it will be gone overnight, but most of the world has moved on.
    Hmmm. I ha'e me doots! Normal (domestic) users don't want to upgrade their computers when XP works fine for them.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,374
    Thanks
    11
    Thanked 592 Times in 572 Posts
    Quote Originally Posted by Philip M View Post
    Hmmm. I ha'e me doots! Normal (domestic) users don't want to upgrade their computers when XP works fine for them.
    again, that's fine; home XP users can easily use a real browser. if they are still using IE8, then their computer doesn't "work fine for them" if they want to use major consumer sites like youtube, netflix, facebook, etc...

    IE8 is about 5% and falling. what's the argument for double-coding everything? "it may be 5% now, but will be 3% next year, so let's go for it!" ?
    this is why big players have moved on; it's a dis-service to the 90%+ of your users with real devices to waste valuable dev time mollycoddling a long-dead browser.

    i admit, a few years ago it made sense, 10-20% is not negligible and IE8 wasn't budging. it's small now and has been shrinking steadily for about a year.
    Last edited by rnd me; 06-24-2014 at 06:38 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,079
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    The page I linked to gives XP as 25% of OS's.

    FWIIW I use IE10 - and am entirely satisfied with it. Chrome is fine as well, but I do not see any particular advantage.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Quote Originally Posted by Philip M View Post
    FWIIW I use IE10 - and am entirely satisfied with it. Chrome is fine as well, but I do not see any particular advantage.
    For Web developers, there is a big advantage. As a developer, I develop in Chrome first then test in the rest of the browsers.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •