41.判断一个元素是否在数组中
export const contains = (arr, val) => { return arr.indexOf(val) != -1 ? true : false; }
42.数组排序
{type} 1:从小到大 2:从大到小 3:随机 export const sort = (arr, type = 1) => { return arr.sort((a, b) => { switch (type) { case 1: return a - b; case 2: return b - a; case 3: return Math.random() - 0.5; default: return arr; } }) }
43.去重
export const unique = (arr) => { if (Array.hasOwnProperty('from')) { return Array.from(new Set(arr)); } else { var n = {}, r = []; for (var i = 0; i < arr.length; i++) { if (!n[arr[i]]) { n[arr[i]] = true; r.push(arr[i]); } } return r; } }
44.求两个集合的并集
export const union = (a, b) => { var newArr = a.concat(b); return this.unique(newArr); }
45.求两个集合的交集
export const intersect = (a, b) => { var _this = this; a = this.unique(a); return this.map(a, function (o) { return _this.contains(b, o) ? o : null; }); }
46.删除其中一个元素
export const remove = (arr, ele) => { var index = arr.indexOf(ele); if (index > -1) { arr.splice(index, 1); } return arr; }
47.将类数组转换为数组
export const formArray = (ary) => { var arr = []; if (Array.isArray(ary)) { arr = ary; } else { arr = Array.prototype.slice.call(ary); }; return arr; }
48.最大值
export const max = (arr) => { return Math.max.apply(null, arr); }
49.最小值
export const min = (arr) => { return Math.min.apply(null, arr); }
50.求和
export const sum = (arr) => { return arr.reduce((pre, cur) => { return pre + cur }) }
51.平均值
export const average = (arr) => { return this.sum(arr) / arr.length }
52.去除空格
export const trim = (str, type) => { type = type || 1 switch (type) { case 1: return str.replace(/\s+/g, ""); case 2: return str.replace(/(^\s*)|(\s*$)/g, ""); case 3: return str.replace(/(^\s*)/g, ""); case 4: return str.replace(/(\s*$)/g, ""); default: return str; } }
53.字符转换
type: 1:首字母大写 2:首字母小写 3:大小写转换 4:全部大写 5:全部小写 export const changeCase = (str, type) => { type = type || 4 switch (type) { case 1: return str.replace(/\b\w+\b/g, function (word) { return word.substring(0, 1).toUpperCase() + word.substring(1).toLowerCase(); }); case 2: return str.replace(/\b\w+\b/g, function (word) { return word.substring(0, 1).toLowerCase() + word.substring(1).toUpperCase(); }); case 3: return str.split('').map(function (word) { if (/[a-z]/.test(word)) { return word.toUpperCase(); } else { return word.toLowerCase() } }).join('') case 4: return str.toUpperCase(); case 5: return str.toLowerCase(); default: return str; } }
54.检测密码强度
export const checkPwd = (str) => { var Lv = 0; if (str.length < 6) { return Lv } if (/[0-9]/.test(str)) { Lv++ } if (/[a-z]/.test(str)) { Lv++ } if (/[A-Z]/.test(str)) { Lv++ } if (/[\.|-|_]/.test(str)) { Lv++ } return Lv; }
55.函数节流器
export const debouncer = (fn, time, interval = 200) => { if (time - (window.debounceTimestamp || 0) > interval) { fn && fn(); window.debounceTimestamp = time; } }
56.在字符串中插入新字符串
export const insertStr = (soure, index, newStr) => { var str = soure.slice(0, index) + newStr + soure.slice(index); return str; }
57.判断两个对象是否键值相同
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; }
58.16进制颜色转RGBRGBA字符串
export const colorToRGB = (val, opa) => { var pattern = /^(#?)[a-fA-F0-9]{6}$/; //16进制颜色值校验规则 var isOpa = typeof opa == 'number'; //判断是否有设置不透明度 if (!pattern.test(val)) { //如果值不符合规则返回空字符 return ''; } var v = val.replace(/#/, ''); //如果有#号先去除#号 var rgbArr = []; var rgbStr = ''; for (var i = 0; i < 3; i++) { var item = v.substring(i * 2, i * 2 + 2); var num = parseInt(item, 16); rgbArr.push(num); } rgbStr = rgbArr.join(); rgbStr = 'rgb' + (isOpa ? 'a' : '') + '(' + rgbStr + (isOpa ? ',' + opa : '') + ')'; return rgbStr; }
59.追加url参数
export const appendQuery = (url, key, value) => { var options = key; if (typeof options == 'string') { options = {}; options[key] = value; } options = $.param(options); if (url.includes('?')) { url += '&' + options } else { url += '?' + options } return url; }