1.forEach()方法,用于循环遍历整个数组
该方法的参数是一个回调函数,回调函数可以传两个参数,第一个参数是数组中的每一项元素,
第二个参数是每一项元素对应的下标。
注意:第二个参数可以省略。
<script>
let users = [
{ name: "张含韵", "email": "zhang@email.com" },
{ name: "江一燕", "email": "jiang@email.com" },
{ name: "李小璐", "email": "li@email.com" }];
let res=users.map(r=>r.email)
console.log(res.join(", "));//zhang@email.com, jiang@email.com, li@email.com
</script>
2.filter()方法,用于过滤数组中的元素,返回过滤结果。
<script>
let arr = [32, 33, 16, 40];
let res = arr.filter(r=>r>18);
console.log(res);//32,33,40
</script>
3.find()方法,用于获取数组中满足规则的第一个元素
<script>
let arr = [1, 2, 3, 4, 5, 6, 7];
let res = arr.find(r=>r>3)
console.log(res);//4
</script>
4.findIndex()方法,用于获取数组中满足规则的第一个元素下标
<script>
let arr = [5, 12, 8, 130, 44];
let res=arr.findIndex(r=>r>13)
console.log(res);//3
</script>
5.some()方法,用于表示数组中是否有满足指定规则的元素,有返回true,一个都没有返回false
<script>
let arr = [222,34,56,7,744]
let res=arr.some(r=>r>30)
let res1=arr.some(r=>r<3)
console.log(res);//true
console.log(res1);//false
</script>
6.every()方法,用于表示数组中是否所有元素都满足指定的规则
<script>
let arr = [222,34,56,7,744]
let res=arr.every(r=>r>5)
let res1=arr.every(r=>r>56)
console.log(res);//true
console.log(res1);//false
</script>
7.map()方法,用于将原始数组里面的数据根据指定的规则返回新的数组
<script>
let techerList = [
{ techerId: 't1', techerName: '张三', sex: '男', email: '123456789@qq.com' },
{ techerId: 't2', techerName: '李四', sex: '女', email: '123456789@qq.com' },
{ techerId: 't3', techerName: '王五', sex: '男', email: '123456789@qq.com' },
{ techerId: 't4', techerName: '赵六', sex: '女', email: '123456789@qq.com' },
{ techerId: 't5', techerName: '孙七', sex: '男', email: '123456789@qq.com' }
]
let newTecherList = techerList.map(item => {
return item.techerId
})
console.log(newTecherList); //["t1","t2","t3","t4","t5",]
</script>
8.sort()方法,对数组的元素进行排序
注:回调函数需要传两个参数,返回参数1-参数2是升序,返回参数2-参数1是降序
<script>
//对象
let obj = [ { age: 24 }, {age: 48 }, { age: 12},]
console.log(obj.sort((a, b) => a.age - b.age)) // {age: 12},{age: 24},{age: 48} 这样正序
console.log(obj.sort((a, b) => b.age - a.age)) // {age: 48},{age: 24},{age: 12} 这样倒叙
9.reduce()方法,统计数组中元素的值(从左到右)
<script>
let arr1 = [1, 2, 3, 4, 5]
let sum = arr1.reduce((pre, item) => {
return pre + item
}, 0)
console.log('sum=', sum); //15
</script>
10.reduceRight()方法,统计数组中元素的值(从右到左)
<script>
let arr1 = [1, 2, 3, 4, 5]
let sum = arr1.reduceRight((pre, item) => {
return pre + item
}, 0)
console.log('sum=', sum); //15
</script>
</body>