JavaScriptの中で循環してArayとMapの方法を遍歴して結びます.
3928 ワード
js循環配列の様々な方法eg 1:
for (var i = 0; i < myStringArray.length; i++) {
alert(myStringArray[i]);
//Do something
}
eg 2:
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]);}
eg 3:
[1,2,3,4].map( function(item) {
alert(item);
})
eg 4:
var x = [1,2,3,4].map( function(item) { return item * 10; } );
// And now x is [10,20,30,40]
eg 5:
var myStringArray = [ "Hello", "World" ]
var len = myStringArray.length
for (var i=0; i
eg 6:
var myStringArray = [ "Hello", "World" ]
myStringArray.forEach( function(s) {
... do something with s ...
} )
eg 7:
var i=0,item,items = ['one','two','three'];
while(item=items[i++]){
console.log(item);
}
// logs: 'one','two','three'
and for the reverse order, an even more efficient loop
var items = ['one','two','three'], i=items.length;
while(i--){
console.log(items[i]);
}
// logs: 'three','two','one'
or the classical for loop
var items = ['one','two','three']
for(var i=0,l=items.length; i < l ; i++){
console.log(items[i]);
}
// logs: 'one','two','three'
eg 8:
var myStringArray = ['Hello', 'World']; // array uses [] not {}
for (var i in myStringArray) {
console.log(i + ' -> ' + myStringArray[i]); // i is the index/key, not the item
}
jsサイクルmapは、すべてのkeyとvalue eg 1を取得する:
//Page
pageUrl : {
menu : "loadPage.htm?url=/collect/menu.page", //
guangfaPage : "loadPage.htm?url=/collect/menu.page", //
pinganPage : "loadPage.htm?url=/collect/menu.page", //
nuonuoPage : "loadPage.htm?url=/collect/menu.page", //
youbangPage : "loadPage.htm?url=/collect/menu.page", //
inputMobileNo : "loadPage.htm?url=/collect/inputMobileNo.page", //
readIdCard : "loadPage.htm?url=/collect/readIdCard.page", //
member : "loadPage.htm?url=/collect/member.page", //
bankCard : "loadPage.htm?url=/collect/bankCard.page", //
url : "loadPage.htm?url=/collect/url.page" // url
},
for(var key in this.pageUrl){
alert(key+" : "+this.pageUrl[key]);
}
eg 2:
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])
}
}
二重Mapサイクルeg:
var msg = "";
for(var key in Pin) {
for(var i in Pin[key]){
msg+=i+": "+Pin[key][i]+"
";
}
}
alert(msg);