Discuss / JavaScript / 遍历数组练习

遍历数组练习

Topic source

廖i雪

#1 Created at ... [Delete] [Delete and Lock User]
// 遍历数组
var arr_names = ['Bart', 'Lisa', 'Adam'];
// 正序
for (var i in arr_names) {
console.log(`Hello, ${arr_names[i]}!`);
}
// 正序 方法2 es6 for...of
for(var value of arr_names) {
console.log(`Hello, ${value}!`)
}
// 遍历数组 逆序
var a = arr_names.length - 1; // 注意 - 1,不然遍历时会越界
// console.log(arr_names[a])
while (a >= 0) {
// console.log('Hello,' , arr_names[a] ,'!'); 或
console.log(`Hello, ${arr_names[a]}!`);
a--;
}

  • 1

Reply