在Javascript中循环遍历数组的元素

时间:2022-10-19 13:56:39

I have an array in Javascript:

我在Javascript中有一个数组:

var array = new array();
array[0] = "apples";
array[1] = "oranges";
array[2] = "pears";

In PHP, with a given array, I can use the following to loop through an array, and break up the keys and values:

在PHP中,使用给定的数组,我可以使用以下内容遍历数组,并分解键和值:

foreach ($array as $key => $value) {
    echo("Key is $key and Value is $value");
}

How can I do this in Javascript? I'm aware of:

我怎么能用Javascript做到这一点?我知道:

for (x in array){
    // Do something with x.
}

But I've found nothing that will replicate the php style foreach. Is it possible to concisely achieve something similar in Javascript? (I'm also using jQuery, if something can be done in jQuery).

但我发现没有什么可以复制php风格的foreach。是否有可能简洁地在Javascript中实现类似的东西? (我也使用jQuery,如果可以在jQuery中完成某些事情)。

6 个解决方案

#1


14  

First,

var array=[];

is preferable to using "new."

比使用“新”更可取。

Second, your keys are numeric in this case, so you just do:

其次,在这种情况下,你的键是数字的,所以你只需:

for (i=0;i<array.length;i++) {
  console.log("Key is "+i+" and Value is "+array[i]);
}

If you want to have keys that aren't numeric, use a JavaScript object instead of an array. It's valid to use strings instead of numbers as array indexes, but JavaScript doesn't have much support for that.

如果要使用非数字键,请使用JavaScript对象而不是数组。使用字符串而不是数字作为数组索引是有效的,但JavaScript对此没有太多支持。


I use "console.log" because I assume you don't want a bunch of alerts popping up. console.log could be replaced with whatever you use to log info. You could use alert() or write text into a div instead.

我使用“console.log”因为我假设您不希望弹出一堆警报。 console.log可以替换为用于记录信息的任何内容。您可以使用alert()或将文本写入div。

#2


8  

Using jQuery.each you could write something similar to (not tested):

使用jQuery.each,您可以编写类似于(未测试)的内容:

jQuery.each(array, function(k,v) {
    console.log("K: "+,k," V:",v);
});

#3


1  

Have a look at underscorejs.org - with it you do it this way:

看看underscorejs.org - 用它你这样做:

_.each(array, function(element, index, array) {
    doSomething(item, index);
});

It is used by backbonejs and many other frameworks/libraries, like meteor, e.g. It has 80 or so extremely useful functions - if you are serious about javascript, take 30 min to read the whole underscore page, you will never want to code in anything but javascript.

它由backbonejs和许多其他框架/库使用,例如meteor,例如它有80个左右非常有用的功能 - 如果你认真对待javascript,花30分钟阅读整个下划线页面,你永远不会想要除了javascript之外的任何代码。

#4


1  

for (x in array){

     var arrayItem = array[x]

}

This type of for loop does work, but gives the array position rather than the item from the array itself. It is quite concise and I use it all the time

这种类型的for循环确实有效,但是给出数组位置而不是数组本身的项。它非常简洁,我一直都在使用它

#5


0  

If order isn't a priority (or even if it is, you could always just reverse the array), here's my preferred method:

如果顺序不是优先级(或者即使它是,你总是可以反转数组),这是我的首选方法:

var i = array.length;
while(i--) {
  console.log("key is " + i + " and value is " + array[i]);
}

This approach works because the number 0 evaluates as false in JavaScript.

这种方法有效,因为数字0在JavaScript中的计算结果为false。

#6


0  

jQuery is not needed for this kind of task, it can just use a for loop that it's advisable way to loop through an Array in JS. You can read this link text for more detailed info

这种任务不需要jQuery,它可以只使用for循环,这是在JS中循环数组的可取方法。您可以阅读此链接文本以获取更多详细信息

#1


14  

First,

var array=[];

is preferable to using "new."

比使用“新”更可取。

Second, your keys are numeric in this case, so you just do:

其次,在这种情况下,你的键是数字的,所以你只需:

for (i=0;i<array.length;i++) {
  console.log("Key is "+i+" and Value is "+array[i]);
}

If you want to have keys that aren't numeric, use a JavaScript object instead of an array. It's valid to use strings instead of numbers as array indexes, but JavaScript doesn't have much support for that.

如果要使用非数字键,请使用JavaScript对象而不是数组。使用字符串而不是数字作为数组索引是有效的,但JavaScript对此没有太多支持。


I use "console.log" because I assume you don't want a bunch of alerts popping up. console.log could be replaced with whatever you use to log info. You could use alert() or write text into a div instead.

我使用“console.log”因为我假设您不希望弹出一堆警报。 console.log可以替换为用于记录信息的任何内容。您可以使用alert()或将文本写入div。

#2


8  

Using jQuery.each you could write something similar to (not tested):

使用jQuery.each,您可以编写类似于(未测试)的内容:

jQuery.each(array, function(k,v) {
    console.log("K: "+,k," V:",v);
});

#3


1  

Have a look at underscorejs.org - with it you do it this way:

看看underscorejs.org - 用它你这样做:

_.each(array, function(element, index, array) {
    doSomething(item, index);
});

It is used by backbonejs and many other frameworks/libraries, like meteor, e.g. It has 80 or so extremely useful functions - if you are serious about javascript, take 30 min to read the whole underscore page, you will never want to code in anything but javascript.

它由backbonejs和许多其他框架/库使用,例如meteor,例如它有80个左右非常有用的功能 - 如果你认真对待javascript,花30分钟阅读整个下划线页面,你永远不会想要除了javascript之外的任何代码。

#4


1  

for (x in array){

     var arrayItem = array[x]

}

This type of for loop does work, but gives the array position rather than the item from the array itself. It is quite concise and I use it all the time

这种类型的for循环确实有效,但是给出数组位置而不是数组本身的项。它非常简洁,我一直都在使用它

#5


0  

If order isn't a priority (or even if it is, you could always just reverse the array), here's my preferred method:

如果顺序不是优先级(或者即使它是,你总是可以反转数组),这是我的首选方法:

var i = array.length;
while(i--) {
  console.log("key is " + i + " and value is " + array[i]);
}

This approach works because the number 0 evaluates as false in JavaScript.

这种方法有效,因为数字0在JavaScript中的计算结果为false。

#6


0  

jQuery is not needed for this kind of task, it can just use a for loop that it's advisable way to loop through an Array in JS. You can read this link text for more detailed info

这种任务不需要jQuery,它可以只使用for循环,这是在JS中循环数组的可取方法。您可以阅读此链接文本以获取更多详细信息