2

The code is very simple and I would be expecting true however it returns false

var markets = ["AB", "CD"];
console.log("AB" in markets);
Hoa
  • 19,858
  • 28
  • 78
  • 107
  • 2
    Because this is not how the `in` operator works: https://developer.mozilla.org/en/JavaScript/Reference/Operators/in – Felix Kling Mar 05 '12 at 21:39
  • If you are developing for any browser but IE you could use Array.indexOf also a bit more of a complete solution would be to use Jquery's utility method inArray http://stackoverflow.com/questions/237104/array-containsobj-in-javascript – scrappedcola Mar 05 '12 at 21:42

7 Answers7

3

I think you're meaning if (markets.indexOf('AB') !== -1). in essentially checks if the test is a property of the object, not if an element is contained within the array.

For more information, look at Array.indexOf vs. the in operator.

Brad Christie
  • 100,477
  • 16
  • 156
  • 200
  • +1 for offering the solution. I wasn't willing to go the extra mile. :) –  Mar 05 '12 at 21:42
  • @cwolves: Indeed, however the article (for `Array.indexOf`) has the source for implementing it when it doesn't exist. – Brad Christie Mar 05 '12 at 21:46
1

Because in looks up property names, not values. Your property names are the array indices.

1

From MDN's page on the in operator:

The in operator returns true if the specified property is in the specified object.

prop A string or numeric expression representing a property name or array index

Note a property name or array index. The in operator does not search for property values, but for property names. In this case, the property names are 0 and 1, so 0 in markets will return true.

You should use indexOf, in browsers that support it, and shim it in those that don't.

Community
  • 1
  • 1
lonesomeday
  • 233,373
  • 50
  • 316
  • 318
0

Because in is meant for objects, not arrays. If you want to reliably do this you have to search through each element in the array:

for( var i=0, l=markets.length; i<l; i++ ){
    if( markets[i] === 'AB' ){
        // do something
    }
}

The following will work, which is what you're thinking:

var markets = { AB: 1, CD: 1 };
console.log( "AB" in markets );
  • 1
    Hmmm... I can't agree with your first sentence. An Array is an Object, and it is just as useful for Arrays objects as it is for any other objects. In both cases it will test the existence of a property name instead of a property value. –  Mar 05 '12 at 21:48
  • @amnotiam -- I was simplifying the concepts :) For the purposes of understanding this why the OP's code doesn't work, `Array != Object`. And it's not just as useful for Array objects as writing `aryVar.foo = 'bar'` is just bad form for 99% of people since they don't realize what's actually going on. –  Mar 05 '12 at 22:50
0

In only works when you are using an object, not an array. So this will work:

var markets = {
    AB: 'AB',
    CD: 'CD'
};

'AB' in markets; // true
David G
  • 94,763
  • 41
  • 167
  • 253
0

As said in won't help you in this case. I guess you'll have to write a searching function. Here's one:

function inArray(ArrObj, Search){
  var exists = false;
  for (var i = 0; i < ArrObj.length; i++){
    if (ArrObj[i] == Search){
      return true;
      var exists = true;
      break;
    }
    else if ((i == (ArrObj.length - 1)) && (!exists)){
      if (ArrObj[i] != Search){
        return false;
      }
    }
  }
}
Ofir Baruch
  • 10,323
  • 2
  • 26
  • 39
-1

I think you want something like this, console.log(markets[0]);

skipZero
  • 162
  • 10