dcsimg
www.webdeveloper.com
Results 1 to 3 of 3

Thread: how to check if an array contains a specific value?

Hybrid View

  1. #1
    Join Date
    Oct 2012
    Posts
    2

    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. #2
    Join Date
    Mar 2009
    Posts
    523
    Something like this should do it. If you pass the array of objects as the first arg and the key you are looking for as the second it should return the number of elements in the array that possess such a key, as in :
    var quantity = checkKey(YourArray, 'A');

    function checkKey(Arr, key){
    var number = 0;
    var i, len = Arr.length;
    for(i = 0; i < len; i++){
    if(Arr[i][key]){
    number++;
    }
    return number;
    }

  3. #3
    Join Date
    Oct 2012
    Posts
    2
    good,thx

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles