for(var i in aArray)VS for(i = 0; i <aArray.length; i ++) [英] for(var i in aArray) VS for(i=0; i&lt;aArray.length; i++)

查看:321
本文介绍了for(var i in aArray)VS for(i = 0; i <aArray.length; i ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想问一下 in_array_orig in_array_new 是否一样。
,当我比较这两个数组( aArr1 vs aArr2 )时,我也会对结果感到困惑。

有人可以解释我吗?谢谢

以下是我的示例测试代码

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


函数in_array_orig(oItem,aArray)
{
for(var i = 0; i< aArray.length; i ++)if(aArray [i] == oItem)返回true;

返回false;


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

返回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)); //假

预先感谢

解决方案

如果属性在对象中,中的返回true。这包括通过原型链查找。例如:

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

即使 f 空的对象,它的原型(包括JS中的所有类型)包含 Object ,它具有属性'k'

虽然你没有问,这里检查对象自己的属性的一个有用的函数是 .hasOwnProperty(),所以在上面的例子中:

  f.hasOwnProperty('k'); //假,这就是我们所期望的

以迭代所有的属性,因为这些属性可能包括除了索引值之外的东西,所以出于性能和预期行为的原因,对于(var i = 0; i< n; i ++)的正则 code $>

因此,如果你正在使用数组,请使用 in_array_orig ,和对你的属性感兴趣的对象,可以使用 in_array_new (应该正确地重命名, in_obj 或其他) 。
$ b

另外, [1] == [1] 返回false,因为两个对象/数组不是相同对象。事实上,他们的每一个属性和索引都具有相同的价值,尽管他们并不是坐在同一个地方,因此不被认为是平等的。您可以轻松构建(或在网上找到)深层搜索 equals()例程检查两个对象/数组是否确实等于的值(而不是地址)。


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

解决方案

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

这篇关于for(var i in aArray)VS for(i = 0; i <aArray.length; i ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆