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?

3
  • 1
    Possibly duplicate: stackoverflow.com/questions/143847 (may not work when looking for true and false though). Commented Apr 7, 2010 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. Commented Apr 7, 2010 at 10:33
  • Felix, can you answer, that to i can flag your answer like a decision. Commented Apr 7, 2010 at 10:45

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

3
for(key in arr){
  var value = arr[key];
  if(!value){
    //do stuff
  }
}

Comments

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.

Comments

0

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

$.inArray(false, arr);

It returns -1 if no such matching found

Comments

-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
    }
}

6 Comments

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.
@TJ, Looping through an array is always a couple of examples away from "Hello World". So no, I'm not being sarcastic.
Not, i'm not new in JS. But i finding way to not write myself if there is found decision.
@Anurag: Reads sarcastic to me (and apparently someone else, but just one someone else). (Edit: Okay, so now it's two other people.)
@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 jsfiddle.com
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.