views:

128

answers:

4

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

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] + ']');
}
John Feminella
I get: "value at index [3] is: [undefined]"
Adam
for ( **var** i=0
Justin Johnson
had var in my case:) still undefined... :(
Adam
@Adam: Is `myArr.length` equal to 4 or more? If so, then you're putting things into your array which shouldn't be there (assuming you didn't expect to see that value be undefined). It would help a lot if you pasted some code.
John Feminella
A: 
for ( i = 0; i < miArray.length; i++ ){ 
    alert( 'value number ' + myArr[i] );
}
Ben
A: 

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
    }    
}
Sean Kinsey
Hmm -- this code doesn't actually do anything, so an optimizing Javascript engine will remove it entirely.
John Feminella
myArr[i]; is a perfectly valid expression, but as you say - a good engine will probably optimize it away :)
Sean Kinsey
A: 

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

ItzWarty
This kind of *JavaScript (TM)* features, such as `yield`, are not part of the [ECMAScript](http://www.ecma-international.org/publications/standards/Ecma-262.htm) Standard, they are available only on *Mozilla implementations* (SpiderMonkey, Rhino). IMO, other engines, such as V8 (Chrome), JavaScripCore (WebKit), etc... will never support those non-standard features.
CMS