1

I just want to ask if the in_array_orig and in_array_new is just the same. and also im confused on the result when comparing both array (aArr1 vs aArr2).

can someone explain me. thanks

Here is my sample test code

function echo(s)
{
    document.write(s);
}

function in_array_orig(oItem, aArray)
{   
    for (var i=0; i<aArray.length; i++) if (aArray[i] == oItem) return true;

    return false;
}

function in_array_new(oItem, aArray)
{
    for (var i in aArray) if (aArray[i] == oItem) return true;

    return false;
}

var a = ['gab', '24', 'manila'];

var aArr1 = [1];
var b = {0:aArr1, 1:24, 2:'manila'};

var aArr2 = [1];


echo(in_array_orig(24, a)); // true
echo(in_array_new(24, b)); // true

echo(in_array_orig(aArr2, b)); // false
echo(in_array_new(aArr2, b)); // false

echo ((aArr1==aArr2)); // false
echo ((aArr1===aArr2)); // false

thanks in advance

ilovekhym
  • 105
  • 1
  • 1
  • 9

6 Answers6

6

The in operator returns true if the property is in the object. This includes a lookup right up through the prototype chain. For example:

Object.prototype.k = 5;
f = {};
'k' in f; // true

Even though f is an empty object, it's prototype (as all types in JS) includes that of Object, which has the property 'k'.

Although you didn't ask, a useful function here to check the object's own properties only, is .hasOwnProperty(), so in our example above:

f.hasOwnProperty('k'); // false, that's more what we would expect

Although for arrays you don't (usually) want to iterate over all properties, since these properties may include things other than index values, so both for reasons of performance and expected behaviour, a regular for(var i=0;i<n;i++) should be used.

As such, if you're using arrays go with in_array_orig, and for objects where you are interested in their properties, use in_array_new (which should be renamed appropriately, in_obj or something).

In addition, [1] == [1] returns false since the two objects/arrays are not the same object. Indeed each of their properties and indexes have the same value, although they aren't sitting at the same place in memory, and thus are not considered equal. You can easily build (or find on the net) a deep search equals() routine to check whether two objects/arrays are indeed equal in value (as opposed to address).

Community
  • 1
  • 1
davin
  • 44,863
  • 9
  • 78
  • 78
0

I guess this is already discussed here in SO.

See it here

Community
  • 1
  • 1
ace
  • 6,775
  • 7
  • 38
  • 47
0

in_array_orig will work only with arrays, in_array_new will also work with dictionaries. For arrays, their results will be the same, but there's no guarantee that items will be processed in correct order in in_array_new (in case of this function it doesn't matter).

About array comparison, it seems that JavaScript doesn't compare arrays element-by-element, but rather compares if the array is the same object in memory, e.g.:

aArr1 == aArr1 // true
Dzinx
  • 55,586
  • 10
  • 60
  • 78
0

Yes, there are differences between the two.

Firstly, the syntax for(var i=0; i<aArray.length; i++) will obviously only work correctly for arrays which only have numeric keys, and where there aren't any gaps in the key sequence. If that doesn't apply to your data, then this syntax will obviously give you problems.

But the real differences come down to how Javascript treats arrays and objects.

Using for(x in y) causes JS to loop through all properties and methods in an object.

In JS, an array is simply a type of object, with a few pre-defined methods and properties, such as length to find the number of array elements, pop() and push() to add and remove elements, etc.

If you use the for(x in y) syntax on an array, it will include some of these methods and properties in the loop. This is unlikely to be what you intended, and could cause some odd side effects.

Therefore, if you want to use for(x in y), you are better off starting with an object rather than an array.

I hope that helps.

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • nice. thank you very much for this info. actually we have a util class that has in_array so im confused if my updated in_array that can also handle objects is just the same as the old. – ilovekhym Apr 01 '11 at 08:47
  • -1 no it does not include pop & push. These have their enumerable property set to false. – Raynos Apr 01 '11 at 08:47
0

To your first question, pls refer http://www.openjs.com/articles/for_loop.php

About the second:

echo(in_array_orig(24, a)); // true
echo(in_array_new(24, b)); // true

I think this won't be a problem to you.

echo(in_array_orig(aArr2, b));
echo(in_array_new(aArr2, b));

Both of these would definitely be false, becuz they are no where inside b. If you make a small change in b:

var b = {0:aArr1, 1:24, 2:'manila', 3:[1]};

then try this:

console.log(in_array_new(aArr2, b[3]));
console.log(in_array_orig(aArr2, b[3]));

they are both True now.

Finally, the last:

echo ((aArr1==aArr2));

"==" operator compares their value, so this is false because they clearly bears different content, meaning value.

echo ((aArr1===aArr2));

"===" compares their value and type, their values are different but their types are the same, you can check it by:

console.log(typeof aArr1===typeof aArr2);

thus it is still a False.

Linmic
  • 761
  • 4
  • 12
-1

if the in_array_orig and in_array_new is just the same

Yes, they are. (same behavior)

im confused on the result when comparing both array (aArr1 vs aArr2)

We can't compare array like this: aArr1 == aArr2

RobertO
  • 2,655
  • 1
  • 20
  • 18