JS代码片段
js时间转换
javascript
/**
*时间格式化
* @param data 需要格式化的时间字符串
*/
function formatDate(data: string) :string {
const time = new Date(data);
const formatDate = {
year: time.getFullYear(), // 年
month: time.getMonth() + 1, // 月份
date: time.getDate(), // 日
hours: time.getUTCHours() < 10 ? `0${time.getUTCHours()}` : time.getUTCHours(), // 小时
minutes: time.getMinutes() < 10 ? `0${time.getMinutes()}` : time.getMinutes(), // 分
seconds: time.getSeconds(), // 秒
};
return `${formatDate.year}-${formatDate.month}-${formatDate.date} ${formatDate.hours}:${formatDate.minutes}`;
}
输入框只输入number
javascript
oninput="value=value.replace(/[^\d]/g,'')"
校验非法字符
javascript
const regExp = /^[\u4e00-\u9fa5A-Za-z0-9_-]+[\))(,;\(\.\%。\u4e00-\u9fa5A-Za-z0-9_-]?$/;
if (!regExp.test(value)) {
callback(new Error('含有非法字符,请重新输入'));
return null;
}
脱敏
javascript
// 手机号
tel.substring(0,3)+'****'+tel.substring(tel.length-4,tel.length)
// 身份证
idCard.replace(/^(.{6})(?:\d+)(.{4})$/, '\$1******\$2')
根据数组id 筛选另外一个数组
js
const n = []
const map = store.state.recipe.types.reduce((acc, obj) => {
acc[obj.id] = obj;
return acc;
}, {});
const queryList = []
n.forEach(item=>{
queryList.push(map[item])
})