首页>>前端>>JavaScript->实用的 JavaScript 单行函数

实用的 JavaScript 单行函数

时间:2023-11-29 本站 点击:0

本文整理了一些实用的 JavaScript 单行函数,特别好用,能让你更快的完成工作,抽出更多的时间陪你的女朋友。话不多说,我们开始吧!

时间和日期

检查日期是否合法

constisDateValid=(...val)=>!Number.isNaN(newDate(...val).valueOf())console.log(isDateValid("December17,199503:24:00"))//true

检查日期是否为周末

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一

查找日期位于一年中的第几天

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//272

计算2个日期之间相差多少天

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//1

时间处理

consttimeFromDate=date=>date.toTimeString().slice(0,8)console.log(timeFromDate(newDate()))console.log(timeFromDate(newDate(2021,0,10,17,30,0)))//14:23:36//17:30:00

数字

检查数字是偶数还是奇数

constisEven=(num)=>num%2===0console.log(isEven(5))//false

多个数字的平均值

constaverage=(...args)=>args.reduce((a,b)=>a+b)/args.lengthconsole.log(average(1,2,3,4,5))//3

两个数字之间生成一个随机数

constrandom=(min,max)=>Math.floor(Math.random()*(max-min+1)+min)console.log(random(1,50))//couldbeanythingfrom1-50

字符串

翻转字符串

constreverse=str=>str.split('').reverse().join('')console.log(reverse('helloworld'))//dlrowolleh

首字母大写字符串

constcapitalize=(str)=>str.charAt(0).toUpperCase()+str.slice(1)console.log(capitalize('helloworld'))

在结尾处截断字符串

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一0

从中间截断字符串

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一1

生成随机字符串(唯一ID)

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一2

数组

获取数组中的唯一值(数组去重)

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一3

检查变量是否为数组

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一4

打乱数组

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一5

校验数组是否为空

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一6

合并多个数组的不同方法

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一7

布尔值

随机获取布尔值

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一8

切换布尔

constisWeekend=(date)=>[0,6].indexOf(date.getDay())!==-1console.log(isWeekend(newDate(2021,5,21)))//falseconsole.log(isWeekend(newDate(2021,5,20)))//true//当前日期为2021.6.21周一9

变量交换

交换两个变量(分号必须有)

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2720

短路

Longhand

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2721

Shorthand

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2722

获取 JavaScript 语言的实际类型

方法 A

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2723

方法 B

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2724

颜色

颜色RGB转十六进制

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2725

生成随机十六进制颜色

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2726

Cookie

获取浏览器Cookie的值

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2727

清除全部Cookie

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2728

从 URL 获取查询参数

方法 A

constdayOfYear=(date)=>Math.floor((date-newDate(date.getFullYear(),0,0))/1000/60/60/24)//三个参数输入具体日期月份要减一console.log(dayOfYear(newDate(2021,8,29)))console.log(dayOfYear(newDate("2021-9-29")))console.log(dayOfYear(newDate()))//272//272//2729

方法 B

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//10

移动端

检查用户是否在Apple设备上

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//11

检查用户的设备是否处于黑暗模式

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//12

其他

滚动到页面顶部

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//13

将文字复制到剪贴板

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//14

获取用户选择的文本

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//15

检查当前选项卡是否在视图/焦点内

constdayDif=(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)console.log(dayDif(newDate("2021-10-1"),newDate("2021-9-30")))//16


本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:/JavaScript/722.html