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]+);
-
Could you please post out your code, it would help us better understand it.Mr.Expert– Mr.Expert2010年05月23日 16:56:13 +00:00Commented May 23, 2010 at 16:56
7 Answers 7
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] + ']');
}
3 Comments
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.for ( i = 0; i < miArray.length; i++ ){
alert( 'value number ' + myArr[i] );
}
Comments
Instead of loop through, use built-in function:
function myFunction() {
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.valueOf();
alert(x);
}
Comments
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
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.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
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
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