最新更新时间:2021年09月16日09:57:13
《猛戳-查看我的博客地图-总有你意想不到的惊喜》
本文内容:
数组
- 一维数组转二维数组,按照N进行分割
function toArr(origin,N){
let res = [];
let cur = [];
origin.forEach((item,i)=>{
if(i % N == 0 && i != 0){
res.push(cur)
cur = []
}
cur.push(item);
});
res.push(cur)
return res
}
//测试用例
let temp = [1,2,3,4,5,6,7,8,9,10];
toArr(temp,1);// [[1],[2],[3],[4],[5],[6],[7],[8],[9],[10]]
toArr(temp,2);// [[1,2],[3,4],[5,6],[7,8],[9,10]]
toArr(temp,3);// [[1,2,3],[4,5,6],[7,8,9],[10]]
toArr(temp,4);// [[1,2,3,4],[5,6,7,8],[9,10]]
- 快速创建数组
new Array(10).fill(3);//[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]
数字
- 生成随机数/字符
function randomNum(min,max) {
return parseInt(Math.random() * ( max - min + 1 ) + min, 10);
}
String.fromCharCode(randomNum(65,122));//65-122 A-z
日期
- 2020-12-08 19:00:12 -> 2020年12月08日19:00:12
//方案一
let date = "2020-12-08 19:00:12";
let res = date.replace(/(\d+)-(\d+)-(\d+)\s/,'$1年$2月$3日');
//方案二
let date = "2020-12-08 19:00:12";
let arr = ['年','月','日']
let count = -1;
let res = date.replace(/-|\s/g,()=>{
count++;
return arr[count]
});
参考资料
- 暂无
感谢阅读,欢迎评论^-^