1、遍历数组的五种方法
while遍历数组for遍历数组for in遍历数组(i是索引)for of遍历数组(i是元素)map遍历数组
2、while遍历数组
var list = ['a', 'b', 'c', 'd'];
// 1、while遍历数组
var i = 0;
while (i < list.length) {
console.log(list[i]);
i++;
}
3、for遍历数组
// 2、for遍历数组
var i = 0;
for (var i = 0; i < list.length; i++) {
console.log(list[i]);
}
4、for in遍历数组(i是索引)
// 3、for in遍历数组(i是索引)
for (var i in list) {
console.log(list[i]);
}
5、for of遍历数组(i是元素)
// 4、for of遍历数组(i是元素)
for (var i of list) {
console.log(i);
}
6、map遍历数组
// 5、map遍历数组
list.map(function (value, index) {
console.log('第' + index + '个值为:' + value);
})
前端面试题库小程序 MST题宝库