1

I have a simple for loop that I need to set up. However instead of iterating through numbers I would like to iterate through:

"A", "B", "1" and then "2"

Is there a way I can do this with just creating an inline array?

6
  • you have to be more specific about the order, if it's just four things - what's the problem with creating an array? You have to specify what you're iterating through somewhere and unless there is a pattern, I don't see how you can avoid writing it down. Commented Jul 21, 2014 at 7:50
  • so where do you store your values then? Commented Jul 21, 2014 at 7:50
  • 2
    Why is an array a problem? It could be as simple as ["A", "B", "1", "2"].forEach(...) Commented Jul 21, 2014 at 7:50
  • You could do Array.prototype.forEach.call('AB12', ...), but why? Commented Jul 21, 2014 at 7:51
  • 1
    Well, just have a look at the manual... Commented Jul 21, 2014 at 7:53

1 Answer 1

2

As far as I understand, you would like to replace the usage of for() loop

var i;
var array = ["A", "B", "1", "2"];
for (i = 0; i < array.length; ++i) {
    alert(array[i]);
}

You can do that by simply using forEach() function

var array = ["A", "B", "1", "2"];
array.forEach(function(value) {
    alert(value);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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