分享一下我工作中常用的一些方法
//普通类型过滤
export function filterNewsType(value){
const ValueMap = {
1: '系统消息',
2: '专利消息',
3: '钱包消息'
}
return ValueMap[value]
}
// 脱敏处理
export function desen (val, type) {
if (typeof val !== 'string') {
val = ''
}
if (type === 'phone') {
let str1 = val.substr(0, 3)
let str2 = val.substr(7, val.length - 1)
val = str1 + '****' + str2
}
if (type === 'idNumber') {
var str1 = val.substring(0, 2)
var str2 = val.substring(6, val.length - 1)
var str3 = str2.substring(0, val.length - 4)
val = str1 + '****' + str3 + '****'
}
return val
}
//除法精度计算
divideFloat(arg1, arg2) {
const arg1Str = arg1 + '';
const arg2Str = arg2 + '';
const arg1StrFloat = arg1Str.split('.')[1] || '';
const arg2StrFloat = arg2Str.split('.')[1] || '';
const m = arg2StrFloat.length - arg1StrFloat.length;
const transferResult = +(arg1Str.replace('.', '')) / +(arg2Str.replace('.', ''));
return transferResult * Math.pow(10, m);;
},
//乘法精度计算
multiplyFloat(arg1, arg2) {
let m = 0;
const arg1Str = arg1 + '';
const arg2Str = arg2 + '';
const arg1StrFloat = arg1Str.split('.')[1];
const arg2StrFloat = arg2Str.split('.')[1];
arg1StrFloat && (m += arg1StrFloat.length);
arg2StrFloat && (m += arg2StrFloat.length);
const transferResult = +(arg1Str.replace('.', '')) * +(arg2Str.replace('.', ''));
return transferResult / Math.pow(10, m);;
}
//四舍五入
roundFloat (value, decimal = 2) {
const n = Math.pow(10, decimal);
return this.divideFloat(Math.round(this.multiplyFloat(value, n)), n).toFixed(decimal);
},
// 数组对象转成树
const toTree = (data, pid) => {
let tree = [];
let temp;
for (let i = 0; i < data.length; i++) {
if (data[i].pid == pid) {
let obj = data[i];
temp = toTree(data, data[i].id);
if (temp.length > 0) {
obj.children = temp;
}
tree.push(obj);
}
}
return tree;
}
//获取网页url问号后面的参数值
export function getQueryString(name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
//判断一个元素是否在数组中(2选1)
export const isExist = (arr, val) => {
return arr.indexOf(val) > -1
}
export const arrIncludeValue = (arr, value) => {
return arr.includes( value)
}
//判断两个对象是否键值相同
export const isObjectEqual = (a, b) => {
var aProps = Object.getOwnPropertyNames(a);
var bProps = Object.getOwnPropertyNames(b);
if (aProps.length !== bProps.length) {
return false;
}
for (var i = 0; i < aProps.length; i++) {
var propName = aProps[i];
if (a[propName] !== b[propName]) {
return false;
}
}
return true;
}
//浅拷贝数组
let a = [1,2,3]
let b = [...a];
console.log(b) //[1,2,3]
//浅拷贝对象
let obj = { name:'a',text:'b',value:1 }
let copys = Object.assign({},obj);
//深拷贝
export function deepClone(source) {
if (!source && typeof source !== 'object') {
throw new Error('error arguments', 'shallowClone');
}
const targetObj = source.constructor === Array ? [] : {};
for (const keys in source) {
if (source.hasOwnProperty(keys)) {
if (source[keys] && typeof source[keys] === 'object') {
targetObj[keys] = source[keys].constructor === Array ? [] : {};
targetObj[keys] = deepClone(source[keys]);
} else {
targetObj[keys] = source[keys];
}
}
}
return targetObj;
}
//数组合并
export const arrConcat = (arrOne, arrTwo) => {
return [...arrOne, ...arrTwo]
}
//判断字符是否包含某值(前面是字符串,后面是包含的值)
export const strInclude = (str, value) => {
return str.includes(value)
}
// 时间格式化
export function formatDate (date, fmt = 'YYYY-MM-DD HH:mm:ss') {
if (!(date = toDate(date))) return '';
const o = {
'M+': date.getMonth() + 1, // 月份
'(D|d)+': date.getDate(), // 日
'h+': date.getHours() % 12 == 0 ? 12 : date.getHours() % 12, // 小时
'H+': date.getHours(), // 小时
'm+': date.getMinutes(), // 分
's+': date.getSeconds(), // 秒
'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
'S+': date.getMilliseconds() // 毫秒
};
if (/(Y+)/i.test(fmt)) {
fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
}
if (/(E+)/.test(fmt)) {
fmt = fmt.replace(RegExp.$1, ['日', '一', '二', '三', '四', '五', '六'][date.getDay()]);
}
Object.keys(o).forEach(k => {
if (new RegExp('(' + k + ')').test(fmt)) {
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('0'.repeat(RegExp.$1.length) + o[k]).substr(('' + o[k]).length)));
}
});
return fmt;
}
//密码验证 (包含数字字母 长度为6-->16)
export const isPassword = password => {
let jubPas = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/
if (jubPas.test(password)) return true
else return false
}
//连接两个数组,返回值是连接后的新数组
arr.concat()
//遍历数组(对原数组的值进行操作),返回一个新数组
arr.map(callbak)
//过滤数组,返回一个满足要求的数组
arr.filter(callback)
// 根据判断条件,遍历数组中的元素,是否都满足,若都满足则返回true,反之返回false
arr.every(callback)
//根据判断条件,遍历数组中的元素,是否存在至少有一个满足,若存在则返回true,反之返回false
arr.some(callback)