1、随机生成字符串

const randomStr = () =>
Math.random().toString(20).slice(2);
randomStr();

2、实现字符串的翻转

const reverseStr = str =>
str.split('').reverse().join('');
reverStr('vue 入门到精髓');
reverStr('it技术分享技术');

3、删除数组中重复元素

const set = new Set([12,356,56,56,85,45,12,12,13,16,18]);
console.log([...set]);

4.RGB到十六进制转换机制

const rgbToHex = (r,g,b)=>"#"+((1<<24)+(r<<16)+(g<<8)+b).toString(16).slice(1);
rgbToHex(255,255,255);

5、打乱一个数组,重新组合

const shuffle = array =>array.sort(()=>0.6-Math.random());
shuffle([29,3,56,45,13,40]);

6、获取两个日期的时间间隔(天数)

const dayDif =(date1,date2)=>Math.ceil(Math.abs(date1.getTime()-date2.getTime())/86400000)
dayDif(new Date("2022-09-03"),new Date("2023-08-10"))

7、获取当前属于今年的第几天

const dayofYearStr = (date)=> Math.floor((date-new Date(date.getFullYear(),0,0))/1000/60/60/24);
dayofYearStr(new Date());

8、截取字符串长度

const splitStr = (string,length)=>string.length < length ? sting:`${string.slice(0,length-3)}...`;
splitStr('vue is good web frame',7);
splitStr('vue is good web frame',10);

9、判断数组是否为空

const isNotEmptyArr = arr=>Array.isArray[arr] && arr.length > 0;
isNotEmptyArr([6,66,666]);
isNotEmptyArr([]);

10、合并两个数组为一个数组

const merge = (a,b) => a.concat(b);
const a =[1,2,3];
const b = [4,5,6];
merge(a,b);

11、英文句子首字母大写切换

const capitalizeEverWordStr = str =>str.replace(/\b[a-z]/g,char =>char.tpU[[erCase());
capitalizeEverWordStr('hellow vue is');                                                                           

12、数组删除元素

const dropArr = (arr,n=1)=>arr.slice(n);
dropArr([10,20,30,40,50]);
dropArr([10,20,30,40,50],2);
dropArr([10,20,30,40,50],42);

13、获取当前日期的时间部分

const getTimeFromDate = date=> date.toTimeString().slice(0,8);
getTimeFromDate(new Date());

14、判断是否是json字符串

const isJson = str =>
try{
Json.parse(str);
    return true;
}catch (e){
return false
}};
isJson('{"name":"小明","address":"苏州"}');
isJson('{"name":"小王","address":"南京"}');

15、获取当前网址

const currentURl = ()=>
window.location.href;
currentURl();

 

posted on 2023-08-10 14:35  是程序喵哇  阅读(14)  评论(0编辑  收藏  举报