2

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]+);
1
  • Could you please post out your code, it would help us better understand it. Commented May 23, 2010 at 16:56

7 Answers 7

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] + ']');
}
Sign up to request clarification or add additional context in comments.

3 Comments

I get: "value at index [3] is: [undefined]"
had var in my case:) still undefined... :(
@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.
2
for ( i = 0; i < miArray.length; i++ ){ 
    alert( 'value number ' + myArr[i] );
}

Comments

2

Instead of loop through, use built-in function:

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

Comments

0

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

1 Comment

This kind of JavaScript (TM) features, such as yield, are not part of the ECMAScript 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.
0

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
    }    
}

2 Comments

Hmm -- this code doesn't actually do anything, so an optimizing Javascript engine will remove it entirely.
myArr[i]; is a perfectly valid expression, but as you say - a good engine will probably optimize it away :)
0

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

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

1 Comment

This could work and makes sense only if the sole purpose of the loop is to increment the counter. The loop mentioned could exist for the purpose of doing additional work with items in the array.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.