迭代稀疏数组 [英] Iterating sparse arrays

查看:73
本文介绍了迭代稀疏数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果一个数组是稀疏的,请说类似


var foo = [];

foo [3] = 4;

foo [''bar''] ='''baz'';

foo [''quux''] ='''moo'';


有没有办法迭代整个阵列?


-

Christopher Benson-Manica |我*应该*知道我在说什么 - 如果我

ataru(at)cyberspace.org |不,我需要知道。火焰欢迎。

解决方案

Christopher Benson-Manica写道:

如果一个数组是稀疏的,请说像

var foo = [];
foo [3] = 4;
foo [''bar''] ='''baz'';
foo [''quux ''] ='''moo'';

有没有办法迭代整个数组?



< snip>


for(var c = foo.length; c--;){

... //使用foo [c]

}


索引0到2的值将返回未定义的值。命名的

属性与Array对象的Arrayness无关,而是与它的Objectness相关,而不是
。因此,他们不应该被认为是想要迭代整个阵列的想法。


Richard。


>如果一个数组很稀疏,可以说像


var foo = [];
foo [3] = 4;
foo [''bar''] ='''baz'';
foo [''quux''] ='''moo'';

有没有办法迭代整个数组?




当键是非整数时使用数组是不正确的。如果你有
有像''bar''和''quux'这样的键,你应该使用一个对象。您可以

然后使用for..in语句迭代它们。


var foo = {};

foo [3] = 4;

foo.bar =''baz'';

foo [''quux''] ='''moo'';


for(key in foo){

...

}


见< a rel =nofollowhref =http://www.crockford.com/javascript/survey.html\"target =_ blank> http://www.crockford.com/javascript/survey.html


文章< cs ********** @ chessie.cirr.com>,

Christopher Benson-Manica <在*** @ nospam.cyberspace.org>写道:

如果数组稀疏,请说

var foo = [];
foo [3] = 4;
foo [''bar''] ='''baz'';
foo [''quux''] ='''moo'';

有没有办法迭代通过整个数组?




是。


注意:length属性包括数字索引。由于三个是最高指数

,因此长度属性的值为四。


罗伯特


< ;!DOCTYPE HTML PUBLIC" - // W3C // DTD HTML 4.01 Transitional // EN"

" http://www.w3.org/TR/html4/loose.dtd"> ;

< html>

< head>

< title>稀疏数组< / title>

< script type =" text / javascript">


var foo = [];

foo [3] = 4 ;

foo [''bar''] ='''baz'';

foo [''quux''] ='''moo'';


var accumulate ="" ;;


for(var i in foo)

{

accumulate + =" foo [" + i +"] =" + foo [i] +" ;; " ;;

}


提醒(累积);


提醒(&f; foo中的元素数量) =" + foo.length);

< / script>

< / head>

< body>

< p>演示检索所有设定的数组值。< / p>

< / body>


If an array is sparse, say something like

var foo=[];
foo[3]=4;
foo[''bar'']=''baz'';
foo[''quux'']=''moo'';

is there a way to iterate through the entire array?

--
Christopher Benson-Manica | I *should* know what I''m talking about - if I
ataru(at)cyberspace.org | don''t, I need to know. Flames welcome.

解决方案

Christopher Benson-Manica wrote:

If an array is sparse, say something like

var foo=[];
foo[3]=4;
foo[''bar'']=''baz'';
foo[''quux'']=''moo'';

is there a way to iterate through the entire array?


<snip>

for (var c = foo.length; c--; ){
... // using foo[c]
}

The values at indexes 0 to 2 will return Undefined values. The named
properties are not related to the Arrayness of the Array object but
rather to its Objectness. As such they should not be considered relevant
to a desire to iterate through the "entire array".

Richard.


> If an array is sparse, say something like


var foo=[];
foo[3]=4;
foo[''bar'']=''baz'';
foo[''quux'']=''moo'';

is there a way to iterate through the entire array?



It is incorrect to use an array when the keys are non-integers. If you
have keys like ''bar'' and ''quux'', you should be using an object. You can
then use the for..in statement to iterate through them.

var foo = {};
foo[3] = 4;
foo.bar = ''baz'';
foo[''quux''] = ''moo'';

for (key in foo) {
...
}

See http://www.crockford.com/javascript/survey.html


In article <cs**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:

If an array is sparse, say something like

var foo=[];
foo[3]=4;
foo[''bar'']=''baz'';
foo[''quux'']=''moo'';

is there a way to iterate through the entire array?



Yes.

Note: The length property includes the numeric indexes. Since three is
the highest index, the length property has a value of four.

Robert

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sparse array</title>

<script type="text/javascript">

var foo=[];
foo[3]=4;
foo[''bar'']=''baz'';
foo[''quux'']=''moo'';

var accumulate = "";

for (var i in foo)
{
accumulate += "foo["+ i + "] = " + foo[i] + "; ";
}

alert(accumulate);

alert("number of elements in foo = " + foo.length);
</script>
</head>
<body>
<p>Demonstrate retrieving all set array values.</p>
</body>


这篇关于迭代稀疏数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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