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

asked Apr 1, 2011 at 8:22
2
  • What language are you using? JavaScript? Add it as tag please. Commented Apr 1, 2011 at 8:24
  • opps, sorry about that. thanks. Commented Apr 1, 2011 at 8:28

6 Answers 6

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).

answered Apr 1, 2011 at 8:45
Sign up to request clarification or add additional context in comments.

Comments

0

I guess this is already discussed here in SO.

See it here

answered Apr 1, 2011 at 8:39

2 Comments

hello thanks for the link. i cant find those links on my search keys so i just decided to post a question.
That's OK the importance is, you now understand its differences.
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
answered Apr 1, 2011 at 8:42

Comments

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.

answered Apr 1, 2011 at 8:44

2 Comments

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.
-1 no it does not include pop & push. These have their enumerable property set to false.
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.

answered Apr 1, 2011 at 8:51

Comments

-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

answered Apr 1, 2011 at 8:41

7 Comments

-1 What about extending Array.prototype? There not the same behaviour
I mean in_array_orig and in_array_new return same result
They dont. See @davin 's answer.
Visible result is same for both.
Array.prototype.foo = 4; in_array_orig(4, []); in_array_new(4, []); They are not the same.
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.