方法一:forEach()方法
var 四大名著 = ['红楼梦', '西游记', '三国演义', '水浒传'];
四大名著.forEach(function(value, index, obj) {
console.log("[" + index + "] = " + value);
})
方法二:for...in...方法
var 水浒传 = ['宋江', '李逵', '武松', '林冲'];
for (var n in 水浒传) {
console.log(水浒传[n]);
}
方法三:for循环
for (var n = 0; n < 水浒传.length; n++) {
console.log(水浒传[n]);
}
本文介绍了使用JavaScript遍历数组的三种常见方法:forEach()方法、for...in...方法及传统的for循环。每种方法都有其特点和适用场景,通过实际代码示例详细解释了它们的用法。
2416

被折叠的 条评论
为什么被折叠?



