最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loop through array, print with string - Stack Overflow

programmeradmin5浏览0评论

I have a simple array like:

var myArr=["one","two","three"];

an I have a counting loop, which increases the value of var i by one.

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]+);

I have a simple array like:

var myArr=["one","two","three"];

an I have a counting loop, which increases the value of var i by one.

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]+);
Share Improve this question edited May 23, 2010 at 19:23 Justin Johnson 31.3k7 gold badges66 silver badges89 bronze badges asked May 23, 2010 at 16:51 Adam TalAdam Tal 9215 gold badges14 silver badges36 bronze badges 1
  • Could you please post out your code, it would help us better understand it. – Mr.Expert Commented May 23, 2010 at 16:56
Add a comment  | 

7 Answers 7

Reset to default 11

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 alert line is removed.

for (var i = 0; i < myArr.length; ++i) {
  alert('value at index [' + i + '] is: [' + myArr[i] + ']');
}
for ( i = 0; i < miArray.length; i++ ){ 
    alert( 'value number ' + myArr[i] );
}

Instead of loop through, use built-in function:

function myFunction() {
  var fruits = ["Banana", "Orange", "Apple", "Mango"];
  var x = fruits.valueOf();
  alert(x);
}

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]

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]);

By 'next value', do you mean the value of the current index + 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
    }    
}

The below code will work using arrow functions . Simple and Reduced Code

var myArr=["one","two","three"];
myArr.map((elements) => alert('value number' + " " + elements));

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.

for (var i = 0; i < myArr.length; ++i) {
  alert('Array value at index [' + i + '] :  [' + myArr[i] + ']');
}

for more information

https://www.w3schools.com/js/js_loop_for.asp

发布评论

评论列表(0)

  1. 暂无评论