1.for循环
var list = [1,2,3,4,5];
for (var i = 0; i < list.length; i ++) {
console.log(list[i]);
// 1 2 3 4 5
}
2.foreach遍历(可以同时取出数组中的值和值对应的下标)
let arr = [1,2,3];
arr.forEach(function(i,index){
console.log(i,index)
// 1 0
2 1
3 2
})
3.for --- of遍历
var arr = [1,2,3,4,5];
for(var item of arr){
console.log(item);
//1 2 3 4 5
}
4.for --- in遍历(常见用在对象中,遍历对应的key值和value值)
var person = {name: 'kreme', age: 22, sex: 'man', hobby: 'running'};
for(key in person){
console.log(key,':',person[key]);
}
//输出为:name: 'kreme', age: 22, sex: 'man', hobby: 'running'
5.map循环
var arr = [1,2,3,4,5];
arr.map(function (item) {
console.log(item);
// 1 2 3 4 5
}
6.every循环
var arr = [1,2,3,4,5];
arr.every(function (item) {
console.log(item);
// 1 2 3 4 5
}
7.some循环
var arr = [1,2,3,4,5];
arr.some(function (item) {
console.log(item);
// 1 2 3 4 5
}
8.filter循环
var arr = [1,2,3,4,5];
arr.filter(function (item) {
console.log(item);
// 1 2 3 4 5
}
9.foreach循环
var arr = [1,2,3,4,5];
arr.foreach(function (item) {
console.log(item);
// 1 2 3 4 5
}
最后
以上就是清脆太阳最近收集整理的关于常见的遍历数组和对象的方法的全部内容,更多相关常见内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复