方法
深拷贝
js
const deepCopy = <T,>(obj: T): T => {
if (typeof obj !== 'object' || obj === null) {
return obj
}
let copy: any
if (Array.isArray(obj)) {
copy = []
for (const item of obj) {
copy.push(deepCopy(item))
}
} else {
copy = {} as { [K in keyof T]: T[K] }
for (const key of Object.keys(obj) as (keyof T)[]) {
copy[key] = deepCopy(obj[key])
}
}
return copy
}随机数 生成指定范围和数量的随机数数组
生成随机数
js
// 最小值 最大值 生成数组长度
function generateRandomList(minScore = 0,maxScore = 100,length = 10) {
return Array.from({ length }, () => {
return Math.floor(Math.random() * (maxScore - minScore + 1)) + minScore;
});
}
console.log(generateRandomList(80,100,14));查询地址 指定格式
数据格式[{label:"",children:[]},...]
js
/**
* label:山场社区->>['香洲区', '香洲分局', '翠香街道', '翠香派出所', '山场社区']
* @param {Array} data 数组
* @param {string} label
*/
findAddressByLabel(data, label) {
for (const item of data) {
if (item.label === label) {
return [item.label,...[item.children?item.children:[]]];
}
if (item.children) {
const res = this.findAddressByLabel(item.children, label);
if (res) {
return [ item.label, ...res ];
}
}
}
}查询对象数组中的地址 通过数组中的value获取key
js
const zjgx = {
'万山片区': ['东澳岛', '桂山岛', '大万山岛', '外伶仃岛', '牛头岛'],
'金湾区': ['南水镇', '平沙镇', '红旗镇', '三灶镇'],
'斗门区': ['井岸镇', '白藤街道', '莲洲镇', '斗门镇', '乾务镇', '白蕉镇'],
'高新区': ['唐家湾镇',],
'香洲区': ['翠香街道', '香湾街道', '狮山街道', '梅华街道', '前山街道', '吉大街道', '拱北街道', '南屏镇', '湾仔街道', '凤山街道',],
'横琴粤澳深度合作区': ['横琴深合区'],
'鹤洲洪保十片区': ['桂山镇', '担杆镇', '万山镇']
}
/** 用来对天气预报的片区进行匹配 findKeyByDistrict('平沙', zjgx) --> '金湾区'*/
function findKeyByDistrict(districtName, districtData) {
for (const key in districtData) {
const districts = districtData[key];
for (const district of districts) {
if (district.includes(districtName)) {
return key;
}
}
}
return null; // 如果没有找到匹配的键,可以返回null或其他适当的值
}获取日期列表 生成日期数组
js
/**
* 生成日期数组 日
* createDateList('2023-5-1','2023-5-31') -> ['2023-5-1','2023-5-2',...]
* @param {date|string} start 起始日期
* @param {date|string} end 结束确认
* @returns Array ["20001212"]
*/
export const createDateDayList = (start,end) => {
const timeList = [];// 存储日期列表
const startDate = new Date(start); //起始日期
const endDate = new Date(end);// 结束日期
// 日期校准 否则会拿前一天
startDate.setDate(startDate.getDate() + 1);
endDate.setDate(endDate.getDate() + 1);
// 循环遍历日期范围 天
for (let date = startDate; date <= endDate; date.setDate(date.getDate() + 1)) {
// 格式化日期为 "YYYY-MM-DD" 的字符串
const formattedDate = date.toISOString().split('T')[0];
// 将日期添加到数组中
timeList.push(formattedDate.replaceAll("-", ""));
}
return timeList
}获取日期列表 生成日期数组 月
js
/**
* 生成日期数组 月
* createDateList('2020','2023-5') -> ['2020-01','2020-02',...]
* @param {date|string} start 起始日期
* @param {date|string} end 结束确认
* @returns Array ['2020-01"]
*/
const createDateMonthList = (start, end) => {
const timeList = [];// 存储日期列表
const startDate = new Date(start); //起始日期
const endDate = new Date(end);// 结束日期
// 日期校准 否则会前一月结束
endDate.setMonth(endDate.getMonth() + 1);
// 循环遍历日期范围 月
for (let date = startDate; date <= endDate; date.setMonth(date.getMonth() + 1)) {
console.log(date.toISOString().split('T')[0].slice(0, 7));
// 格式化日期
const formattedDate = date.toISOString().split('T')[0].slice(0, 7);
// 添加到数组中
timeList.push(formattedDate);
}
return timeList
}工具
pinyin -js 拼音
Details
js
// npm install cnchar
const CnChar = require('cnchar');
// array:返回数组;"汉字".spell("array") =>['Han','Zi']
// first:返回首字母 ;"汉字".spell("first") =>'HZ'
// up:将结果全部大写;"汉字".spell("up") =>'HANZI'
// low:将结果全部小写;"汉字".spell("low") =>'hanzi'
// 组合使用:"汉字".spell("first","array",'low') =>['h','z']
// "汉字".stroke() 笔画数 11。
"汉字".spell("first","low")
// 示例用法
// const inputString = '你好World';
// const firstLetter = getFirstLetter(inputString);
// console.log(firstLetter); // 输出 'NHWorld'
// 示例 首字母转小写
// excel.forEach((row,index)=>{
// if (index>0) {row[2] = row[3].spell("first","low")}
// })