开发者

Loop through array, print with string

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 followi开发者_运维问答ng code works, so I'm assuming I'm not calling the counter right:

alert('value number '+myArr[0]+);


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜