js函数方法合集(常用版)

我将js所有的函数方法分为四类:数学函数、日期函数、字符串函数、数组函数

数学函数

  1. 整数: parseInt(number)
    将传入的数字转换为整数

    例1:获取π的整数

    let num = parseInt(3.1415926535) 
    console.log(num)  // 3
    

    例2:是否会四舍五入?
    答:不会

    let num = parseInt(3.9) 
    console.log(num)  // 3
    
  2. 浮点数: parseFloat(number/string)
    可解析一个字符串,并返回一个浮点数

    例1:解析字符串"11月11日"

    let  num = parseFloat("11月11日")
    console.log(num)  // 11
    
  3. 随机数:Math.random()
    产生一个0 - 1之间的随机数

    例1:产生一个随机数

    let num = Math.random()
    console.log(num)  // 0.7994840014784754
    

    例2:取50以内的随机数

    num = Math.random() * 50
    console.log(num)  // 39.88130414891049
    

    例3:取50以内的整数随机数

    let num = parseInt(Math.random() * 50)
    console.log(num)  // 37
    
  4. 最大值:Math.max(number1,number2)
    获取最大值

    例1:输出两个数字中的最大值

    let num = Math.max(5, 10)
    console.log(num)  // 10
    

    例2:输出数组中最大值

    let arr = [1, 3, 5, 10]
    let num = Math.max(...arr) // ...arr 中的...叫扩展运算符,主要作用是将一个数组转为用逗号分隔的参数序列,详情请转至本文的数组函数(1. 扩展运算符)中
    console.log(...arr)  // 1 3 5 10
    console.log(num)  // 10
    
  5. 最小值:Math.min(number1,number2)
    获取最小值

    例1:输出两个数字中的最小值

    let num = Math.min(5, 10)
    console.log(num)  // 5
    

    例2:输出数组中最小值

    let arr = [1, 3, 5, 10]
    let num = Math.min(...arr) // ...arr 中的...叫扩展运算符,主要作用是将一个数组转为用逗号分隔的参数序列
    console.log(...arr)  // 1 3 5 10
    console.log(num)  // 1
    
  6. 四舍五入:round()
    获取一个数字四舍五入后的整数

    例1:将数字进行四舍五入

    const num = Math.round(2.99)
    console.log(num)  // 3
    

日期函数

  1. 详细日期:new Date()
    获取当前日期

    例1:获取当前日期

    let date = new Date()
    console.log(date)  // Mon Feb 13 2023 16:38:53 GMT+0800 (中国标准时间)
    
  2. 时间戳:getTime()
    获取当前日期的时间戳
    时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总秒数

    例1:获取当前日期的时间戳

    let date = new Date()
    let timeStamp = date.getTime()
    console.log(timeStamp)  // 1676277638378
    
  3. 年:getFullYear()
    获取当前年份

    例1:获取当前年份

    let date = new Date()
    let year= date.getFullYear()
    console.log(year)  // 2023
    
  4. 月:getMonth()
    获取当前月份

    例1:获取当前月份

    let date = new Date()
    let month = date.getMonth() + 1
    console.log(month )  // 2
    

    问题: 为什么获取当前月份要+1?
    解答:因为月份是0(一月) 到 11(十二月) 之间的一个整数,获取的是下标,所以要+1

  5. 日:getDate()
    获取当前日

    例1:获取当前日

    let date = new Date()
    let day= date.getDate()
    console.log(day)  // 13
    
  6. 时:getHours()
    获取当前小时

    例1:获取当前小时

    let date = new Date()
    let hour= date.getHours()
    console.log(hour)  // 16
    
  7. 分:getMinutes()
    获取当前分

    例1:获取当前分

    let date = new Date()
    let minute= date.getMinutes()
    console.log(minute)  // 51
    
  8. 秒:getSeconds()
    获取当前秒

    例1:获取当前秒

    let date = new Date()
    let second= date.getSeconds()
    console.log(second)  // 59
    
  9. 毫秒:getMilliseconds()
    获取当前毫秒

    例1:获取当前毫秒

    let date = new Date()
    let millisecond= date.getMilliseconds()
    console.log(millisecond)  // 59
    
  10. 星期: getDay()
    获取当前星期

    例1:获取当前星期

    let date = new Date()
    let weekday= date.getDay()
    console.log(weekday)  // 1
    

字符串函数

  1. 索引:indexof()
    对字符串从左到右依次进行索引,将第一个匹配的字符下标进行返回,如果不包含,返回-1。

    例1:以下字符串中搜索是否包含”老“

    let str = '告诉老墨,我要吃鱼'
    console.log(str.indexOf('老'))  // 2
    
  2. 索引:lastIndexOf()
    对字符串从右往左依次进行索引,将最后一个匹配的字符下标进行返回,如果不包含,返回-1。

    例1:通过indexof与lastIndexOf函数方法,搜索字母‘d’

    let str = 'abcdddefg'
    console.log(str.indexOf('d'))  // 3      
    console.log(str.lastIndexOf('d'))  // 5
    
  3. 指定下标:charAt(index)
    获取字符串中指定下标位置的字符

    例1:获取下标为1的字符

    let str = '告诉老墨,我要吃鱼'
    let value = str.charAt(1)
    console.log(value) // 诉
    
  4. 通过起始下标截取:substring(startIndex, endIndex)
    截取startIndex与endIndex之间的字符串(不包含endIndex)

    例1:截取下标1与3的字符

    let str = '告诉老墨,我要吃鱼'
    let value = str.substring(1, 3)
    console.log(value) // 诉老
    
  5. 通过长度截取:substr(index, length)
    截取从index开始,length长度的字符

    例1:从下标为1处开始,截取3个字符

    let str = '告诉老墨,我要吃鱼'
    let value = str.substr(1, 3)
    console.log(value)  // 诉老墨
    
  6. 替换:replace(oldStr, newStr)
    将字符串中某字符替换

    例1:将以下字符串中的”老“换成“小”

    let str = '告诉老墨,我要吃鱼'
    let value = str.replace('老', '小')
    console.log(value) // 告诉小墨,我要吃鱼
    
  7. 字符串转换为数组:split()
    将字符串按字符拆分为数组

    例1:不传参时

    let str = '告诉老墨,我要吃鱼'
    let value = str.split()
    console.log(value) // ['告诉老墨,我要吃鱼']
    

    例2:参数为 '' 时

    let str = '告诉老墨,我要吃鱼'
    let value = str.split('')
    console.log(value) // ['告', '诉', '老', '墨', ',', '我', '要', '吃', '鱼']
    

    例3:参数为字符串中存在的某字符时,比如”老“

    let str = '告诉老墨,我要吃鱼'
    let value = str.split('老')
    console.log(value) // ['告诉', '墨,我要吃鱼']
    
  8. 转换为小写:toLowerCase()
    将字符串所有内容中字母统一转换为小写

    例1:将以下字符串转换为小写

    let str = 'Hello,world!'
    let value = str.toLowerCase()
    console.log(value)  // hello,world!
    
  9. 转换为大写:toUpperCase()
    将字符串所有内容中字母统一转换为大写

    let str = 'Hello,world!'
    let value = str.toLowerCase()
    console.log(value)  // HELLO,WORLD!    
    

数组函数

  1. 扩展运算符:...arr
    主要作用是将一个数组转为用逗号分隔的参数序列。
    < 它不是函数,但因为本文中数学函数的(4. 最大值)方法中涉及到扩展运算符方法,特在此详细介绍下!>

    例1:用...输出数组

    let arr = [1, 2, 3]
    console.log(...arr)  // 1 2 3
    

    例2:合并两个数组

    let arr1 = [1, 2, 3]
    let arr2 = [4, 5, 6]
    let newArr = [ ...arr1, ...arr2]
    console.log(newArr)  // [1,2,3,4,5,6]
    

    例3:字符串转数组

    let str = "告诉老墨,我想吃鱼"
    console.log([...str])  // ['告', '诉', '老', '墨', ',', '我', '想', '吃', '鱼']
    
  2. 头部添加:unshift(value)
    将数值添加进数组头部
    返回值为 数组长度

    例1:将数字0添加进数组头部

    let arr = [1, 2, 3]
    let newArr = arr.unshift(0)
    console.log(arr)  // [0, 1, 2, 3] 
    console.log(newArr)  // 4
    
  3. 头部删除:shift()
    数组头部删除一个元素,若数组为空,返回undefined
    返回值为 删除的元素

    例1:将数组中第一位的元素删除

    let arr = [ 1, 2, 3]
    let newArr = arr.shift()
    console.log(arr)  // [2, 3] 
    console.log(newArr )  // 1
    
  4. 尾部添加:push()
    将数值添加进数组尾部
    返回值为 数组长度

    例1: 为数组尾部添加数字4

    let arr =[0, 1, 2]
    let newArr = arr.push(4)
    console.log(arr)  // [0,1,2,4]
    console.log(newArr')  // 4
    
  5. 尾部删除:pop()
    数组尾部删除一个元素,若数组为空,返回undefined
    返回值为 删除的元素

    例1:删除数组中尾部元素

    let arr = [0, 1, 2]]
    let newArr = arr.pop()
    console.log(arr)  // [0, 1] 
    console.log(newArr )  // 2
    
  6. 合并数组:concat()
    将数组进行合并

    例1:将以下两个数组进行合并

    let arr1 = [1, 2, 3]
    let arr2 = ['a', 'b', 'c']
    let newArr = arr1.concat(arr2)
    console.log(newArr) // [1, 2, 3, 'a', 'b', 'c']
    
  7. 转字符串:join()
    将数组转换为字符串格式

    例1:将以下数组转换为字符串, 无参数 时

    let arr =  ['a', 'b', 'c']
    let newArr = arr1.join()
    console.log(newArr) // a,b,c
    

    例2:将以下数组转换为字符串, 参数为 '' 时

    let arr =  ['a', 'b', 'c']
    let newArr = arr1.join('')
    console.log(newArr) // abc
    
  8. 颠倒:reverse()
    将数组中元素进行颠倒,原数组 改变

    例1:将以下数组颠倒

    let arr =  ['a', 'b', 'c']
    let newArr = arr.reverse()
    console.log(newArr) // ['c', 'b', 'a']
    console.log(arr) // ['c', 'b', 'a']  
    
  9. 正序排序:sort()
    对数组的元素进行正序排序

    例1: 对以下数组进行正序排序

    const arr = [1, 0, 2, 4]
    const newArr = arr.sort()
    console.log(newArr)  // [0, 1, 2, 4]
    
  10. 截取:slice(startIndex,endIndex)
    根据起始两个参数,对数组截取

    例1:将以下数组进行截取

    let arr = [1, 2, 3, 4, 5]
    let newArr = arr.slice(1, 3)
    console.log(newArr) // [2, 3]
    
  11. 增加/删除数组元素:splice(index, howmany, arr1, arr2…)
    在数组中任意位置增加或删除一个或多个元素,原数组 改变
    返回值为 删除的元素

    参数注解:
    从index位置开始删除howmany个元素,并将arr1、arr2…数据从index位置依次插入。howmany为0时,则不删除元素

    例1:删除多个元素

    let arr = [1, 2, 3, 4, 5]
    let newArr = arr.splice(1, 3)
    console.log(newArr) // [2, 3, 4]
    console.log(arr)  // [1, 5]
    

    例2:给数组增加新元素

    let arr = [1, 2, 3, 4, 5]
    let newArr = arr.splice(5, 0, 6, 7, 8)
    console.log(newArr) // []
    console.log(arr)  // [ 1, 2, 3, 4, 5, 6, 7, 8 ]
    
  12. 遍历 - map()
    调用数组的每一项

    例1:将数组中每一位乘以2

    let arr = [3, 5, 1, 4, 2]
    let newArr = arr.map(item => {
      return item * 2
    })
    console.log(newArr)  // [ 6, 10, 2, 8, 4 ]
    
  13. 遍历 - forEach()
    调用数组的每一项

    与map区别?
    map通过return,给新数组开辟出独立的内存空间,而forEach没有return

    例1:

    let arr = [1, 2, 3, 4, 5]
    let newArr = []
    arr.forEach(item => {
       newArr.push(item * 2)
    })
    console.log(newArr) // [2, 4, 6, 8, 10]
    
  14. 遍历 - 过滤:filter()
    过滤数组中符合条件的元素,并返回一个新的数组
    例1:过滤出数组中小于3的数字

    let arr = [1, 2, 3, 4, 5]
    let newArr = arr.filter(item => item > 3)
    console.log(newArr)  // [4, 5]
    
  15. 遍历 - every()
    对数组中的每一项进行判断,若每项都符合,则返回true,否则返回false

    例1:判断数组中每项元素是否全部大于3

    let arr = [1, 2, 3, 4, 5]
    let newArr = arr.every(item => item > 3)
    console.log(newArr) // false
    
  16. 遍历 - some()
    对数组中的每一项进行判断,若每项都不符合,返回false,否则返回true

    例1:判断数组中每项元素是否全部大于3

    let arr = [1, 2, 3, 4, 5]
    let newArr = arr.some(item => item > 3)
    console.log(newArr) // true
    
  17. 遍历 - 累加器:reduce()
    接受一个函数作为累加器,属于高阶函数

    例1:数组总和

    let arr = [1, 2, 3, 4, 5]
    let total = arr.reduce((a, b) => a + b)
    console.log(total) // 15
    
  18. 遍历 - indexOf()
    检测当前值在数组中第一次出现的位置索引,如果存在,返回下标,不存在返回-1

    例1:寻找数组中是否有数字2,如果有,需返回下标位置

    const arr = [1, 2, 3, 4, 5]
    console.log(arr.indexOf(2))  // 1
    
  19. 遍历 - includes()
    判断一个数组是否包含一个指定的值,返回布尔值

    例1:数组中是否包含数字2

    const arr = [1, 2, 3, 4, 5]
    console.log(arr.includes(2)) // true
    
  20. 以上数组函数中改变原数组的方法有:
    push()、pop()、 shift()、 unshift()、 reverse()、 sort()、 splice()

总结:以上所有函数方法中的例子都是最简单的实例,实际业务中会更加复杂多样。熟能生巧,希望对路过的你有帮助!
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容