JS中的forEach、$.each、map方法推荐
2018-03-27 本文已影响21人
sunnyghx
js循环数组各种方法
1
for (var i = 0; i < myStringArray.length; i++) {
alert(myStringArray[i]);
//Do something
}
Array.prototype.foo = "foo!";
var array = ['a', 'b', 'c'];
for (var i in array) {
alert(array[i]);
}
for(var i in this.$GLOBAL_DETAIL.album_photo_ids){if(this.$GLOBAL_DETAIL.album_photo_ids[i] == "3487675024077108") alert(this.$GLOBAL_DETAIL.album_photo_ids[i]);}
[1,2,3,4].map( function(item) {
alert(item);
})
var x = [1,2,3,4].map( function(item) { return item * 10; } );
// And now x is [10,20,30,40]
var myStringArray = [ "Hello", "World" ]
var len = myStringArray.length
for (var i=0; i<len; ++i) {
if (i in myStringArray) {
var s = myStringArray[i];
... do something with s ...
}
}