Javascript 循环遍历数组,用字符串打印
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2892595/
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
Loop through array, print with string
提问by Adam Tal
I have a simple array like:
我有一个简单的数组,如:
var myArr=["one","two","three"];
an I have a counting loop, which increases the value of var iby one.
我有一个计数循环,它将 var 的值增加i一。
What I want to do is print the next value from the array each time the loop runs, next to a text string, like so:
我想要做的是每次循环运行时打印数组中的下一个值,在文本字符串旁边,如下所示:
alert('value number '+myArr[i]+);
But for some reason I can't get this to work. The following code works, so I'm assuming I'm not calling the counter right:
但由于某种原因,我无法让它发挥作用。以下代码有效,所以我假设我没有正确调用计数器:
alert('value number '+myArr[0]+);
回答by John Feminella
Make sure that you're properly incrementing the loop counter (it should start at zero and run until the highest index in the array), and that the stray +at the end of your alertline is removed.
确保正确递增循环计数器(它应该从零开始并运行直到数组中的最高索引),并且删除了行+尾的杂散alert。
for (var i = 0; i < myArr.length; ++i) {
alert('value at index [' + i + '] is: [' + myArr[i] + ']');
}
回答by Ben
for ( i = 0; i < miArray.length; i++ ){
alert( 'value number ' + myArr[i] );
}
回答by Sri Kanth
Instead of loop through, use built-in function:
而不是循环,使用内置函数:
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.valueOf();
alert(x);
}
回答by DILEEP THOMAS
The below code will work using arrow functions . Simple and Reduced Code
下面的代码将使用箭头函数工作。简单且精简的代码
var myArr=["one","two","three"];
myArr.map((elements) => alert('value number' + " " + elements));
回答by Shafiqul Islam
Your second option alert('value number '+myArr[0]+);because you define array index like 0 in this case working , but myArr[i] not working because i is not define so if assign i = 0 then first value work, but main solution is that use loop and increment counter and get total array use myArr.length , other wise which time loop run you do not know.
你的第二个选择alert('value number '+myArr[0]+);是因为你定义了像 0 这样的数组索引在这种情况下工作,但 myArr[i] 不工作,因为我没有定义所以如果分配 i = 0 那么第一个值工作,但主要的解决方案是使用循环和递增计数器并得到总数组使用 myArr.length ,否则你不知道哪个时间循环运行。
for (var i = 0; i < myArr.length; ++i) {
alert('Array value at index [' + i + '] : [' + myArr[i] + ']');
}
for more information
想要查询更多的信息
回答by Sean Kinsey
By 'next value', do you mean the value of the current index + 1?
“下一个值”是指当前索引的值 + 1?
for (var i=0; i < myArr.length; i++){
console.log(myArr[i]); // value of current index
if (i !== myArr.length - 1) { // if on last index then there is no next value
console.log(myArr[i + 1]); // value of next index
}
}
回答by Warty
Are you thinking of an iterator? This is the upcoming standard, but it is only supported in javascript 1.7+, which in turn is only supported by Firefox as of now, and you'd have to use <script type="application/javascript;version=1.7">... Chrome will claim to support JavaScript 1.7, but it will not actually support anything. [Don't ask me why they did this]
你在考虑迭代器吗?这是即将推出的标准,但仅在 javascript 1.7+ 中受支持,而目前仅受 Firefox 支持,您必须使用<script type="application/javascript;version=1.7">... Chrome 将声称支持 JavaScript 1.7,但它不会实际上支持任何东西。【别问我为什么这么做】
Just for the point of demonstrating it:
只是为了证明它:
function yourIterator(arrayToGoThrough)
{
for(var i = 0; i < arrayToGoThrough.length; i++)
{
yield arrayToGoThrough[i];
}
}
var it = new yourIterator(["lol", "blargh", "dog"]);
it.next(); //"lol"
it.next(); //"blargh"
it.next(); //"dog"
it.next(); //StopIteration is thrown
Note that that is the new standard and you probably don't want to use it =)...
请注意,这是新标准,您可能不想使用它 =)...
You could also "simulate" an iterator like this:
您还可以“模拟”这样的迭代器:
function myIterator(arrayToGoThrough){
this.i = 0;
this.next = function(){
if(i == arrayToGoThrough.length) //we are done iterating
throw { toString: function(){ return "StopIteration"; } };
else
return arrayToGoThrough[i++];
}
}
If you want to use the current standard, you could just iterate through your array
如果你想使用当前的标准,你可以遍历你的数组
for(var i = 0; i < yourArr.length; i++) alert("yourArr["+i+"]: "+yourArr[i]);
for(var i = 0; i < yourArr.length; i++) alert("yourArr["+i+"]: "+yourArr[i]);

