1、for循环
let arr = [1,2,3,4]
for(let j = 0,len=arr.length; j < len; j++) {
console.log(arr[j]);
}
输出结果:1,2,3,4
2、forEach循环
let arr = [1,2,3,4]
arr.forEach((item,index,array)=>{
console.log(index+':'+arr[index]);
})
//参数:value数组中的当前项, index当前项的索引, array原始数组;
//数组中有几项,那么传递进去的匿名回调函数就需要执行几次;
输出结果: 0:1
1:2
2:3
3:4
3、for Of 遍历
var arr = [1,2,3,4,5]
for(let item of arr){
console.log(item)
}
// 1
// 2
// 3
// 4
// 5
4、map循环
var ary = [12,23,24,42,1];
var res = ary.map(function (item,index,ary ) {
return item*10;
})
console.log(res);//-->[120,230,240,420,10]; 原数组拷贝了一份,并进行了修改
console.log(ary);//-->[12,23,24,42,1]; 原数组并未发生变化
map的回调函数中支持return返回值;
并不影响原来的数组,只是相当于把原数组克隆一份,把克隆的这一份的数组中的对应项改变了;
5、filter遍历
var arr = [
{ id: 1, value: 2 },
{ id: 2, value: 4 },
{ id: 2, value: 7 },
]
let newArr = arr.filter(item => item.value>2);
console.log(newArr ,arr )
输出结果:
新数组:
(2) [{…}, {…}]
0: {id: 2, value: 4}
1: {id: 2, value: 7}
原数组:
(3) [{…}, {…}, {…}]
0: {id: 1, value: 2}
1: {id: 2, value: 4}
2: {id: 2, value: 7}
6、every遍历
every()是对数组中的每一项运行给定函数,如果该函数对每一项返回true,则返回true。如果返回false,就退出遍历
var arr = [ 1, 2, 3, 4, 5, 6 ];
if(arr.every( function( item, index, array ){
return item > 3;
})){
console.log("满足条件,每一个都大于3" );
}else{
console.log("不满足条件,不是每一个都大于3" );
}
输出结果:不满足条件,不是每一个都大于3
7、some遍历
some()是对数组中每一项运行指定函数,如果该函数对任一项满足条件,则返回true,就退出遍历;否则返回false。
var arr = [ 1, 2, 3, 4, 5, 6 ];
if(arr.some( function( item, index, array ){
return item > 3;
})){
console.log("");
}else{
console.log("不满足条件,没有大于3的" );
}
输出结果:存在大于3的值
8、reduce
var total = [0,1,2,3,4].reduce((a, b)=>a + b); //10
console.log(total)
输出结果:10
9、reduceRight
reduceRight()方法的功能和reduce()功能是一样的,不同的是reduceRight()从数组的末尾向前将数组中的数组项做累加。
reduceRight()首次调用回调函数callbackfn时,prevValue 和 curValue 可以是两个值之一。如果调用 reduceRight() 时提供了 initialValue 参数,则 prevValue 等于 initialValue,curValue 等于数组中的最后一个值。如果没有提供 initialValue 参数,则 prevValue 等于数组最后一个值, curValue 等于数组中倒数第二个值。
var arr = [0,1,2,3,4];
arr.reduceRight(function (preValue,curValue,index,array) {
console.log(preValue ,curValue)
return preValue + curValue;
}); // 10
输出结果:
4 3
7 2
9 1
10 0
10、find
find()方法返回数组中符合测试函数条件的第一个元素。否则返回undefined
let name= [
{
name: '张三',
gender: '男',
age: 28
},
{
name: '王小毛',
gender: '女',
age: 21
},
{
name: '李四',
gender: '男',
age: 20
}
];
function sun(element){
return element.name == '李四'
}
console.log(name.find(sun)) ;
输出结果:
11、findIndex
对于数组中的每个元素,findIndex 方法都会调用一次回调函数(采用升序索引顺序),直到有元素返回 true。只要有一个元素返回 true,findIndex 立即返回该返回 true 的元素的索引值。如果数组中没有任何元素返回 true,则 findIndex 返回 -1。
findIndex 不会改变数组对象。
let test =[1,2,3]
let test1 = test.findIndex(function (x) {
x == 2;
});
let test2 = test.findIndex((x) => x == 4);
let test3 = test.findIndex((x) => x == 3);
console.log(test1)
console.log(test2)
console.log(test3)
输出结果:-1
-1
2
12、ES6 新方法keys,values,entries
ES6 提供三个新的方法 —— entries(),keys()和values() —— 用于遍历数组。它们都返回一个遍历器对象,可以用for…of循环进行遍历,唯一的区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历
for (let index of ['a', 'b'].keys()) {
console.log(index);
}
// 0
// 1
for (let elem of ['a', 'b'].values()) {
console.log(elem);
}
// 'a'
// 'b'
for (let [index, elem] of ['a', 'b'].entries()) {
console.log(index, elem);
}
// 0 "a"
// 1 "b"
对象遍历
1、使用Object.keys()遍历
返回一个数组,包括对象自身的(不含继承的)所有可枚举属性(不含Symbol属性).
var obj = {'0':'a','1':'b','2':'c'};
Object.keys(obj).forEach(function(key){
console.log(key,obj[key]);
});
输出结果: 0 a
1 b
2 c
2、使用for…in…遍历
循环遍历对象自身的和继承的可枚举属性(不含Symbol属性).
var obj = {'0':'a','1':'b','2':'c'};
for(var i in obj) {
console.log(i,":",obj[i]);
}
输出结果: 0 : a
1 : b
2 : c
3、使用Object.getOwnPropertyNames(obj)遍历
返回一个数组,包含对象自身的所有属性(不含Symbol属性,但是包括不可枚举属性).
var obj = {'0':'a','1':'b','2':'c'};
Object.getOwnPropertyNames(obj).forEach(function(key){
console.log(key,obj[key]);
});
输出结果: 0 : a
1 : b
2 : c
4、使用Reflect.ownKeys(obj)遍历
返回一个数组,包含对象自身的所有属性,不管属性名是Symbol或字符串,也不管是否可枚举.
var obj = {'0':'a','1':'b','2':'c'};
Reflect.ownKeys(obj).forEach(function(key){
console.log(key,obj[key]);
});
输出结果: 0 : a
1 : b
2 : c
5、使用for…of…遍历**
const searchObj = {
title: 'javascript',
author: 'Nicolas',
publishing: "O'RELLY",
language: 'cn',
};
for(let key of searchObj){
console.log(key)
}
// output: title author publishing language
遗漏和错误的请留言或者私信指正!谢谢。