js日常开发中常用的数组函数

1.find() — 寻找符合条件的元素

find()方法 返回符合条件的第一个数组元素值,如果没有符合条件的则返回 undefined。空数组不会调用方法。不会改变原始数组。

!!注意: find() 对于空数组,函数是不会执行的。
!!注意: find() 并没有改变数组的原始值。

参数
currentValue: 必需。当前元素
index: 非必需。当前元素下标值
arr: 非必需。调用当前find方法的数组
thisValue: 非必需。callback方法中this的值

 let arr1=[
            {
                id:1,
                name:'xiaoming'
            },
            {
                id:2,
                name:'lisi'
            }
        ]
        let thisValue=1;
            
        let result1=arr1.find(function(currentValue, index, arr){
           // console.log(this);// 1
          return currentValue.id===1
        },thisValue)
        console.log(result1);//{id:1,name:'xiaoming'}

2.map() — 自定义处理数组元素

map() 方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值,不会对空数组进行检测。不会改变原始数组。

!!注意: map() 不会对空数组进行检测。
!!注意: map() 不会改变原始数组。

参数
currentValue: 必需。当前元素
index: 非必需。当前元素下标值
arr: 非必需。调用当前find方法的数组
thisValue: 非必需。callback方法中this的值

let arr2=[1,2,3,4,5]
let thisValue2=1;
        
let result2=arr2.map(function(currentValue, index, arr){
            // console.log(this);// 1
            return currentValue+'1'
   },thisValue2)
console.log(result2);//['11', '21', '31', '41', '51']

3.reduce() — 对数组元素进行叠加算法(例:计算所有元素的总和)

reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。

!!注意: reduce() 对于空数组是不会执行回调函数的。
!!注意: reduce() 不会改变原始数组。

参数
total: 必需。初始值, 或者计算结束后的返回值
currentValue: 必需。当前元素
index: 非必需。当前元素下标值
arr: 非必需。调用当前find方法的数组
initTotal: 非必需。total的初始值,不填则默认total的初始值为arr[0]的值

let arr3=[1,2,3,4,5]
let initTotal=1;
let result3=arr3.reduce(function(total,currentValue, index, arr){
     return total+currentValue
   },initTotal)
   
console.log(result3);//16

4.some() — 检测数组的元素是否有1个以上符合条件的元素

some() 方法会依次执行数组的每个元素:
如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
如果没有满足条件的元素,则返回false。

!!注意: some() 不会对空数组进行检测。
!!注意: some() 不会改变原始数组。

参数
currentValue: 必需。当前元素
index: 非必需。当前元素下标值
arr: 非必需。调用当前find方法的数组
thisValue: 非必需。callback方法中this的值

let arr4=[1,2,3,4,5]
let thisValue4=1;
let result4=arr4.some(function(currentValue, index, arr){
            // console.log(this);// 1
            return currentValue>4
    },thisValue4)
    console.log(result4);//true

5.every() — 检测数组的元素是否全都符合条件

every() 方法使用指定函数检测数组中的所有元素:
如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
如果所有元素都满足条件,则返回 true。

!!注意: every() 不会对空数组进行检测。
!!注意: every() 不会改变原始数组。

参数
currentValue: 必需。当前元素
index: 非必需。当前元素下标值
arr: 非必需。调用当前find方法的数组
thisValue: 非必需。callback方法中this的值

		let arr5=[11,15,2,27]
        let thisValue5=1;
        let result5=arr5.every(function(currentValue, index, arr){
             //console.log(this);// 1
            return currentValue>3
        },thisValue5)
        console.log(result5);//false

6.filter() — 数组过滤函数

filter() 方法创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。

!!注意: filter() 不会改变原始数组。

参数
currentValue: 必需。当前元素
index: 非必需。当前元素下标值
arr: 非必需。调用当前find方法的数组
thisValue: 非必需。callback方法中this的值

		let arr6=[1,2,3,7]
        let thisValue6=1;
        let result6=arr6.filter(function(currentValue, index, arr){
            //console.log(this);// 1
            return currentValue>2
        },thisValue6)
        console.log(result6);//[3,7]

7.sort() — 数组排序函数

sort() 方法用于对数组的元素进行排序。
排序顺序可以是字母或数字,并按升序或降序。
默认排序顺序为按字母升序。

**!!注意:**当数字是按字母顺序排列时"40"将排在"5"前面。
使用数字排序,你必须通过一个函数作为参数来调用。
函数指定数字是按照升序还是降序排列
**!!注意:**这种方法会改变原始数组!

参数
sortfunction 可选。排序方法。必须是函数。

		let  str=['c','b','d','a']
        str.sort()
        console.log(str);//['a', 'b', 'c', 'd']

        
        

        //升序
        let nums=[1,4,3,5,2]
        let sortfunction1=function(a,b){
            return a-b  
        }
        nums.sort(sortfunction1)//
        console.log(nums);//[1, 2, 3, 4, 5]

        //降序
        let sortfunction2=function(a,b){
            return b-a
        }
        nums.sort(sortfunction2)//
        console.log(nums);//[5, 4, 3, 2, 1]

        // 如果return返回值小于0,表示a小于b,数组排序中a会排在b之前
        // 如果return返回值大于0,表示a大于b,数组排序中a会排在b之后
        // 如果return返回0,表示a与b相等,其位置保持不变
        //升序解析
        let asc=function(a,b){
            if(a>b){  //a比b大,想升序,就是要a排在b的后面,所以这里要返回1 
                return 1  
            }else if(a<b){//a比b小,想升序,就是要a排在b的前面,所以这里要返回-1 
                return -1  
            }else{
                return 0 //a=b  位置保持不变
            }
        }
        //所以函数内部可以简写为  return a-b
        asc=(a,b)=>{return a-b}
        nums.sort(asc);
        console.log(nums);//[1, 2, 3, 4, 5]

        //降序
        let desc=function(a,b){
            if(a>b){  //a比b大,想降序,就是要a排在b的前面,所以这里要返回-1 
                return -1   
            }else if(a<b){//a比b小,想降序,就是要a排在b的后面,所以这里要返回1 
                return 1  
            }else{
                return 0 //a=b  位置保持不变
            }
        }
        //所以函数内部可以简写为  return b-a
        desc=(a,b)=>{return -(a-b)} ;
        desc=(a,b)=>{return b-a} ;
        nums.sort(desc);
        console.log(nums);//[5, 4, 3, 2, 1]

喜欢的点个赞吧,有问题请留言,看到就会回复。多谢观看

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值