JavaScript 数组索引获取方法:for循环与forEach
获取arr索引内容:可以使用for循环来获取arr数组的索引,也可以使用forEach方法来获取:
- 使用for循环获取索引:
for(let i=0; i<arr.length; i++) {
console.log(i); // 输出0, 1, 2, 3, 4, 5
}
- 使用forEach方法获取索引:
arr.forEach(function(item, index) {
console.log(index); // 输出0, 1, 2, 3, 4, 5
});
原文地址: https://www.cveoy.top/t/topic/l4BB 著作权归作者所有。请勿转载和采集!