javascript for(var i in aArray) VS for(i=0; i<aArray.length; i++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5510772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
for(var i in aArray) VS for(i=0; i<aArray.length; i++)
提问by ilovekhym
I just want to ask if the in_array_origand in_array_newis just the same. and also im confused on the result when comparing both array (aArr1vs aArr2).
我只是想问一下in_array_orig和 in_array_new是不是一样的。在比较两个数组(aArr1与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
提前致谢
回答by davin
The in
operator returns true if the property is in the object. This includes a lookup right up through the prototype chain. For example:
该in
运营商如果该属性是在对象返回true。这包括通过原型链直接查找。例如:
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'
.
即使f
是一个空对象,它的原型(就像 JS 中的所有类型一样)包括 of Object
,它具有属性'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:
尽管您没有问,但这里仅检查对象自身属性的有用函数是.hasOwnProperty()
,因此在我们上面的示例中:
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.
尽管对于数组,您(通常)不想迭代所有属性,因为这些属性可能包含索引值以外的内容,因此出于性能和预期行为的原因,for(var i=0;i<n;i++)
应使用常规。
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).
因此,如果您使用数组 with in_array_orig
,并且对于您对其属性感兴趣的对象,请使用in_array_new
(应该适当地重命名,in_obj
或其他)。
In addition, [1] == [1]
returns false since the two objects/arrays are not the sameobject. 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).
此外,[1] == [1]
由于两个对象/数组不是同一个对象,因此返回 false 。实际上,它们的每个属性和索引都具有相同的值,尽管它们不在内存中的相同位置,因此不被视为相等。您可以轻松构建(或在网上找到)深度搜索equals()
例程来检查两个对象/数组的值是否确实相等(与地址相反)。
回答by ace
I guess this is already discussed here in SO.
我想这已经在 SO 中讨论过。
See it here
看这里
回答by DzinX
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).
in_array_orig
仅适用于数组,in_array_new
也适用于字典。对于数组,它们的结果将相同,但不能保证项目将按正确的顺序处理in_array_new
(在此函数的情况下无关紧要)。
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.:
关于数组比较,JavaScript 似乎不会逐个元素比较数组,而是比较数组是否是内存中的同一对象,例如:
aArr1 == aArr1 // true
回答by Spudley
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.
首先,该语法for(var i=0; i<aArray.length; i++)
显然只适用于只有数字键且键序列中没有任何间隙的数组。如果这不适用于您的数据,那么此语法显然会给您带来问题。
But the real differences come down to how Javascript treats arrays and objects.
但真正的区别归结为 Javascript 如何处理数组和对象。
Using for(x in y)
causes JS to loop through all properties and methods in an object.
使用for(x in y)
会导致 JS 循环遍历对象中的所有属性和方法。
In JS, an arrayis 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.
在JS,一个阵列是简单的类型的对象,与一些预定义的方法和属性,例如length
,以找到阵元的数目,pop()
并且push()
添加和删除元件等
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.
如果您for(x in y)
在数组上使用语法,它将在循环中包含其中一些方法和属性。这不太可能是您想要的,并且可能会导致一些奇怪的副作用。
Therefore, if you want to use for(x in y)
, you are better off starting with an objectrather than an array.
因此,如果您想使用for(x in y)
,最好从对象而不是数组开始。
I hope that helps.
我希望这有帮助。
回答by Linmic
To your first question, pls refer http://www.openjs.com/articles/for_loop.php
对于你的第一个问题,请参考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:
这两个肯定是假的,因为它们不在 b 里面。如果你对 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 Truenow.
他们现在都是True。
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.
因此它仍然是False。
回答by atlavis
if the in_array_orig and in_array_new is just the same
如果 in_array_orig 和 in_array_new 是一样的
Yes, they are. (same behavior)
对,他们是。(相同的行为)
im confused on the result when comparing both array (aArr1 vs aArr2)
比较两个数组时,我对结果感到困惑(aArr1 vs aArr2)
We can't compare array like this: aArr1 == aArr2
我们不能像这样比较数组: aArr1 == aArr2