理顺 JavaScript (11) - 数组
2010-09-14 13:40:47 来源:WEB开发网toString、valueOf
var arr;
arr = [1, 2, 3, 'A', 'B', 'C']
alert(arr); //1,2,3,A,B,C
alert(arr.toString()); //1,2,3,A,B,C
alert(arr.valueOf()); //1,2,3,A,B,C
alert(arr.toLocaleString()); //1.00, 2.00, 3.00, A, B, C
遍历与模拟的多维数组
var arr;
arr = [[1,2,3], [4,5,6], [7,8,9]];
alert(arr[1][1]); //5
arr = new Array(new Array(1,2,3), new Array(4,5,6), new Array(7,8,9));
alert(arr[1][1]); //5
alert(arr) //1,2,3,4,5,6,7,8,9
for (i=0; i<arr.length; i++) {
document.write(arr[i] + '<b>'); //1,2,3 / 4,5,6 / 7,8,9
}
arr = [1,2,3,4,5,6,7,8,9];
for (i in arr) {
document.write(arr[i] + '<br>'); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9
}
arr = new Array([1,2,3], [4,5,6], [7,8,9]);
for (i in arr) {
document.write(arr[i] + '<br>'); //1,2,3 / 4,5,6 / 7,8,9
}
for (i in arr) for (j in arr[i])
document.write(arr[i][j] + '<br>'); //1 / 2 / 3 / 4 / 5 / 6 / 7 / 8 / 9
系列文章:
理顺 JavaScript (1) - 准备工具
Tags:理顺 JavaScript 数组
编辑录入:爽爽 [复制链接] [打 印]更多精彩
赞助商链接