1、数据类型检测
typeof 无法准确检测引用类型;instanceof 无法检测基本数据类型,且只要是在原型链上出现过构造函数都会返回true;
所以,使用Object.prototype.toString.call()
function getType (value) {
const str = Object.prototype.toString.call(value);
return str.slice(8, -1);
}
getType(1) // Number
getType({}) // Object
getType([]) // Array
getType(() => {}) // Function
2、手机号检测
到目前为止,手机号第二位的数字有3、4、5、6、7、8、9;
function checkMobile(mobile) {
return /^1[3-9]\d{9}$/.test(mobile);
}
3、金额每n位前加逗号
(1)不使用正则,避免字符串、数组转换:
function formatNumber(num, n) {
let numStr = num.toString();
let result = '';
let decimal = '';
const decimalIndex = numStr.indexOf('.');
if (decimalIndex !== -1) {
decimal = numStr.slice(decimalIndex);
numStr = numStr.slice(0, decimalIndex);
}
while (numStr.length > n) {
result = `,${numStr.slice(-n)}` + result;
numStr = numStr.slice(0, -n);
}
return numStr + result + decimal;
}
(2)使用正则(例如每3位前加逗号):
str.replace(/(\d)(?=(\d{3})+($|\.))/g, '$1,');
4、时间戳转换(10位或13位)
(1)将时间戳转为普通时间;
function formatDateToCommont(timetamp) {
if (!timetamp) {
return "--";
}
if (timetamp.toString().length === 10) {
timetamp = timetamp * 1000;
}
return timetamp.toLocaleString('zh', { hour12: false });
}
formatDateByUnix(1609157288) // 2020/12/28 20:08:08
(2)将普通时间转为时间戳:
const timeUnix = new Date(Date.UTC(year, month - 1, day, hour, minute, second))
5、数组去重
(1)基本数字类型数组去重:
// 不断遍历结果数组去重
function arrayRemoveSame1(array) {
const result = [];
array.forEach(item => {
if (result.indexOf(item === -1)) {
result.push(item);
}
})
return result
}
// 借用对象key去重
function arrayRemoveSame2(array) {
const obj = {};
const result = [];
array.forEach(item => {
if (!obj[item]) {
obj[item] = item;
}
})
for (let key in obj) {
result.push(obj[key]);
}
return result;
}
(2)对象数组去重:对象数组去重往往只需要判断每个元素的唯一key值是否相同(示例以id为例)
// 遍历结果数组去重
function objArrayRemoveSame1(array, name) {
const result = [];
array.forEach(item1 => {
// 用 find() 是因为一旦找到匹配项便会停止遍历
if (!result.find(item2 => item2[name] === item1[name])) {
result.push(item1);
}
})
return result;
}
// 借用对象key去重
function objArrayRemoveSame2(array, name) {
const obj = {};
const result = [];
array.forEach(item => {
if (!obj[item[name]]) {
obj[item[name]] = item;
}
})
for (let key in obj) {
result.push(obj[key]);
}
return result;
}
// 使用:
const array = [{ id: 1, a: 'a' }, { id: 2, a: 'aa' }, { id: 1, a: 'a' }, { id: 3, a: 'ab' }];
objArrayRemoveSame1(array, 'id');
objArrayRemoveSame2(array, 'id');
6、数组浅对比
export function isEqualArray(arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
let equal = true;
const length = arr1.length;
for (let i = 0; i < length; i++) {
if (arr1[i] !== arr2[i]) {
equal = false;
break;
}
}
return equal;
}
7、对象深克隆
(1)利用 JSON 复制:
const newObj = JSON.parse(JSON.stringify(obj));
优点:性能好;
缺点:数据中含有 undefined / function / symbol 类型,无法进行复制;
(2)递归解析
function deepCopy (o, c) {
var c = c || {}
for (let i in o) {
if (typeof o[i] === 'object') {
if (o[i] === null) {
c[i] = o[i]
} else if (o[i] instanceof Array) {
c[i] = []
} else {
c[i] = {}
}
deepCopy(o[i], c[i])
} else {
c[i] = o[i]
}
}
return c
}
// 使用
var obj = {
a : 'a',
b: [1, 2, 3],
}
var newObj = deepCopy(obj, {name:'result'})
8、解析url参数
function getParams(url) {
const indexOfParam = url.indexOf("?");
if (indexOfParam < 0) {
return {};
}
const result = {};
const paramStr = url.slice(indexOfParam + 1);
const paramArr = paramStr.split("&");
for (let item of paramArr) {
const indexOfValue = item.indexOf("=");
const name = item.slice(0, indexOfValue);
const value = item.slice(indexOfValue + 1);
if (value === "") continue;
if (result[name]) {
if (typeof result[name] === "string") {
const firstValue = result[name];
result[name] = [firstValue, value]
} else {
result[name].push(value)
}
} else {
result[name] = value;
}
}
return result;
}
getParams('https://xxx.com/xxx?a=&page=1&status=10&status=21') // { page: ‘1’, status: [10, 21] }
常见问题FAQ
- 免费下载或者VIP会员专享资源能否直接商用?
- 本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
- 提示下载完但解压或打开不了?
- 找不到素材资源介绍文章里的示例图片?
- 模板不会安装或需要功能定制以及二次开发?
发表评论
还没有评论,快来抢沙发吧!