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
-
What language are you using? JavaScript? Add it as tag please.Alin P.– Alin P.2011年04月01日 08:24:16 +00:00Commented Apr 1, 2011 at 8:24
-
opps, sorry about that. thanks.ilovekhym– ilovekhym2011年04月01日 08:28:08 +00:00Commented Apr 1, 2011 at 8:28
6 Answers 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).
Comments
I guess this is already discussed here in SO.
See it here
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
Comments
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.
2 Comments
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.
Comments
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
7 Comments
in_array_orig and in_array_new return same resultArray.prototype.foo = 4; in_array_orig(4, []); in_array_new(4, []); They are not the same.