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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question How to check if an array contains a specific value?

    i have an array like below:
    [{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}]

    how to check if the array elements' key contains "A"?(only key)

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,981
    Thanks
    56
    Thanked 557 Times in 554 Posts
    Code:
    <script type="text/javascript">
    var theArray=[{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}]
    for (var i = 0; i < theArray.length; i++) {
    var pos=0;
    for(b in theArray[i]){
    if(b=="A"){
    alert('"A" found in object '+i+' at position '+pos) 
    		}
    pos++
    	}
    }
    </script>
    Last edited by xelawho; 10-09-2012 at 04:15 AM.

  • #3
    New to the CF scene
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by xelawho View Post
    Code:
    <script type="text/javascript">
    var theArray=[{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}]
    for (var i = 0; i < theArray.length; i++) {
    var pos=0;
    for(b in theArray[i]){
    if(b=="A"){
    alert('"A" found in object '+i+' at position '+pos) 
    		}
    pos++
    	}
    }
    </script>
    what do u think of this one?
    Code:
    var arr = [{"A":"a","B":"b"},{"A":"a","B":"b"},{"C":"c"}] ;
        for(var i=0;i<3;i++)
        {
            if(arr[i].A!=undefined)
               alert('found A');
        }

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    depending on the situation, the array iteration methods are also useful.
    PHP Code:
    var has_A arr.some(function (item) {
        return (
    "A" in item);
    }); 
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    if("A" in theArray)
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by WolfShade View Post
    if("A" in theArray)
    looks cool, does it work?
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    Regular Coder
    Join Date
    Apr 2012
    Location
    St. Louis, MO
    Posts
    985
    Thanks
    7
    Thanked 101 Times in 101 Posts
    It works, for me. I believe as long as it's an associative array, it will work for the key, not the value, I think.
    ^_^

    If anyone knows of a website that can offer ColdFusion help that isn't controlled by neurotic, pedantic jerks* (stackoverflow.com), please PM me with a link.
    *
    The neurotic, pedantic jerks are not the owners; just the people who are in control of the "popularity contest".

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    you are aware that there is no such thing as an "Associative Array" in JavaScript? (that would rather be an Object, lacking all Array methods).

    and though "A" in obj works for objects/associative arrays, it does not for the OP’s variable, which is an Array of Objects, i.e. a nested structure.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  

    Posting Permissions

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