0

I have such an array:

arr['key1'] = true;
arr['key2'] = true;
arr['key3'] = true;
...
arr['keyN'] = true;

How to determine, have anyone key a "false" value?

Ax.
  • 320
  • 3
  • 4
  • 12
  • 1
    *Possibly duplicate:* http://stackoverflow.com/questions/143847/ (may not work when looking for `true` and `false` though). – Felix Kling Apr 07 '10 at 10:29
  • 1
    If you have an array you shouldn't be using non-numeric properties on it. If you need non-numeric properties, use a plain object. – Quentin Apr 07 '10 at 10:33
  • Felix, can you answer, that to i can flag your answer like a decision. – Ax. Apr 07 '10 at 10:45

5 Answers5

8

Unfortunately, the only way to do this until recently was to loop through the array; if you're using this in a browser-based app, you'll probably have to do it that way. There are new array features in ECMAScript 5th edition (the new JavaScript) that let you do this in a slightly different way, but only some browsers support them (and I'm not sure they'd necessarily be applicable).

But what you've described in your question is more a map (or "dictionary;" sometimes called an associative array) than an array (numerically-based indexed thingy). In JavaScript, "array" is usually used to mean numerically-indexed arrays (e.g., created via [] or new Array), and "object" is usually used for maps (because all JavaScript objects are maps). (This can be a bit confusing, because arrays are objects. But don't worry about that.) So this is an array:

var a = ['one', 'two', 'three'];

...whereas this is an object ("map", "dictionary", "associative array"):

var o = {key1: 'one', key2: 'two', key3: 'three'};

Your use of non-numeric indexes suggests you're really using a map, not an array. The search loop looks something like this on maps:

var key, found;

found = false;
for (key in arr) {
    if (arr.hasOwnProperty(key)) {
        if (!arr[key]) {   // <== There are alternatives, see notes below
            found = true;
            break;
        }
    }
}

There I've used if (!arr[key]) to check for the false value. That will actually stop on false, undefined, null, or an empty string. If you really, really want false, use if (arr[key] === false) instead.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
3
for(key in arr){
  var value = arr[key];
  if(!value){
    //do stuff
  }
}
Victor
  • 9,210
  • 3
  • 26
  • 39
2
function hasFalse(arr) {
    for (i in arr) {
        if (!arr[i]) {
            return true;
        }
    }
    return false;
}

This returns as soon as a single false is found.

gub
  • 5,079
  • 3
  • 28
  • 33
0

There is one jquery method to find any value in javascript array

$.inArray(false, arr);

It returns -1 if no such matching found

vikram jeet singh
  • 3,416
  • 2
  • 21
  • 22
-1
  1. loop through each value in the array (for-loop)
  2. check the value to see if it's false.

are you new to Javascript?

for(var i = 0; i < arr.length; i++) {
    if(arr[i] === false) {
        // do something
    }
}
Anurag
  • 140,337
  • 36
  • 221
  • 257
  • 7
    No need to be sarky, he's probably wondering if there's a built-in array function that will do it for him. Not an unreasonable thing to hope for. – T.J. Crowder Apr 07 '10 at 10:32
  • @TJ, Looping through an array is always a couple of examples away from "Hello World". So no, I'm not being sarcastic. – Anurag Apr 07 '10 at 10:40
  • 1
    Not, i'm not new in JS. But i finding way to not write myself if there is found decision. – Ax. Apr 07 '10 at 10:42
  • 1
    @Anurag: Reads sarcastic to me (and apparently someone else, but just one someone else). *(Edit: Okay, so now it's two other people.)* – T.J. Crowder Apr 07 '10 at 10:42
  • @Ax.. there are some great answers available here. I suggest you start with simple answers, and test all others from there. If you want an online tool to quickly test a piece of Javascript code, then checkout http://www.jsfiddle.com – Anurag Apr 07 '10 at 11:04
  • @TJ, so what you're saying is that you have three accounts on SO? – Anurag Apr 07 '10 at 11:07