In Java you can use a for()
loop to go through objects in an array like so:
String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
//Do something
}
can you do the same in JavaScript?
In Java you can use a for()
loop to go through objects in an array like so:
String[] myStringArray = {"Hello","World"};
for(String s : myStringArray)
{
//Do something
}
can you do the same in JavaScript?
In javascript the only similar syntax is:
for(var i in myStringArray){
var s = myStringArray[i];
}
Use a sequential for
loop:
var myStringArray = ["Hello","World"];
for (var i = 0; i < myStringArray.length; i++) {
alert(myStringArray[i]);
//Do something
}
@zipcodeman suggests the use of the for...in
statement, but for iterating arrays for-in
should be avoided, that statement is meant to enumerate object properties.
It shouldn't be used for array-like objects because:
The second point is can give you a lot of problems, for example, if you extend the Array.prototype
object to include a method there, that property will be also enumerated.
For example:
Array.prototype.foo = "foo!";
var array = ['a', 'b', 'c'];
for (var i in array) {
alert(array[i]);
}
The above code will alert, "a", "b", "c" and "foo!".
That be particularly a problem if you use some library that relies heavily on native prototypes augmention (such as MooTools for example).
The for-in
statement as I said before it's there to enumerate object properties, for example:
var obj = {
"a": 1,
"b": 2,
"c": 3
};
for (var prop in obj) {
if (obj.hasOwnProperty(prop)) {
// or if (Object.prototype.hasOwnProperty.call(obj,prop)) for safety...
alert("prop: " + prop + " value: " + obj[prop])
}
}
In the above example the hasOwnProperty
method allows you to enumerate only own properties, that's it, only the properties that the object physically has, no inherited properties.
I would recommend you to read the following article:
You can use map
(also known as apply
in other languages like python, and probably haskell too)
[1,2,3,4].map( function(item) {
alert(item);
})
The general syntax is:
array.map(func)
func
should take one parameter.
The return value of array.map
is another array, so you can use it like this:
var x = [1,2,3,4].map( function(item) { return item * 10; } );
And now x is [10,20,30,40]
I must clarify: this concept is from the functional paradigm.
You don't have to write the function inline; one might do so as a first sketch, but you could then extract it into its own function.
var item_processor = function(item) {
// do something complicated to an item
}
new_list = my_list.map(item_processor);
which would be sort-of equivalent to:
for(item in my_list) { item_porcessor(item); }
except you don't get the new_list
.
Opera, Safari, Firefox and Chrome now all share a set of enhanced Array methods for optimizing many common loops.
You may not need all of them, but they can be very useful, or would be if every browser supported them.
The mozilla labs published the algorithms they and webkit both use, so that you can add them yourself.
filter returns an array of items that satisfy some condition or test.
every returns true if every array member passes the test.
some returns true if any pass the test.
forEach runs a function on each array member and doesn't return anything.
map is like forEach, but it returns an array of the results of the operation for each element.
These methods all take a function for their first argument, and have an optional second argument, which is an object whose scope you want to impose on the array members as they loop through the function.
Ignore it until you need it.
indexOf and lastIndexOf find the appropriate position of the first or last element that matches its argument exactly.
(function(){
var p, ap= Array.prototype, p2={
filter: function(fun, scope){
var L= this.length, A= [], i= 0, val;
if(typeof fun== 'function'){
while(i< L){
if(i in this){
val= this[i];
if(fun.call(scope, val, i, this)){
A[A.length]= val;
}
}
++i;
}
}
return A;
},
every: function(fun, scope){
var L= this.length, i= 0;
if(typeof fun== 'function'){
while(i<L){
if(i in this && !fun.call(scope, this[i], i, this)) return false;
++i;
}
return true;
}
return null;
},
forEach: function(fun, scope){
var L= this.length, i= 0;
if(typeof fun== 'function'){
while(i< L){
if(i in this){
fun.call(scope, this[i], i, this);
}
++i;
}
}
return this;
},
indexOf: function(what, i){
i= i || 0;
var L= this.length;
while(i< L){
if(this[i]=== what) return i;
++i;
}
return -1;
},
lastIndexOf: function(what, i){
var L= this.length;
i= i || L-1;
if(isNaN(i) || i>= L) i= L-1;
else if(i< 0) i += L;
while(i> -1){
if(this[i]=== what) return i;
--i;
}
return -1;
},
map: function(fun, scope){
var L= this.length, A= Array(this.length), i= 0, val;
if(typeof fun== 'function'){
while(i< L){
if(i in this){
A[i]= fun.call(scope, this[i], i, this);
}
++i;
}
return A;
}
},
some: function(fun, scope){
var i= 0, L= this.length;
if(typeof fun== 'function'){
while(i<L){
if(i in this && fun.call(scope, this[i], i, this)) return true;
++i;
}
return false;
}
}
}
for(p in p2){
if(!ap[p]) ap[p]= p2[p];
}
return true;
})();