数组

数组遍历


0:宝马
1:奔驰
2:宾利

function myFunction() {

    var person = {fname:"John", lname:"Doe", age:25}; 
    
    var text = "";
    var txt   = "";
    var x;
    for (x in person) {
        text += x+":"+person[x] + "
";
txt += x+",";
    }
    document.getElementById("demo").innerHTML = text; //
fname:John lname:Doe age:25



    document.getElementById("demo1").innerHTML = txt;  //fname,lname,age,
}


for方法:

var arr = [0,1,2,3,4,5];

for(var i = 0;i

consle.log(arr[i])

}

输出:0 1 2 3 4 5


for in 方法:

var arr1 = [1,2,5,8];

for(var x in arr1){

console.log(arr1[x]);

}

输出:1 2 5 8


forEach方法: //es5.1标准

var arr2=[1,2,3,4,5,6];

arr2.forEach(function(v,i){ //v==value 为arr2项;i == index为 arr2索引

console.log(v)

})

输出:1 2 3 4 5 6 ; i为0 1 2 3 4 5


for of方法: //es6标准

var arr3=[1,2,3,4,5,6];

for(var value of arr3){

console.log(value)

}

输出:1 2 3 4 5 6

你可能感兴趣的:(javascript)