使用reduce,自己看着改
let arr = [
{id: 1, name: '周瑜'},
{id: 3, name: '王昭君'},
{id: 2, name: '亚瑟'},
{id: 1, name: '小乔'},
{id: 2, name: '大桥'},
{id: 3, name: '韩信'}
];
let obj = {}
arr = arr.reduce((preVal,curVal) => {
obj[curVal.id] ? "" : obj[curVal.id] = preVal.push(curVal)
return preVal
},[])
console.log(arr)
第一个参数是输入的时间,标准格式或时间戳格式都可
第二个参数是输出的样子,要啥写啥样,全部是这样 ‘yyyy-MM-dd hh:mm:ss’
export function formatDate(t, format='yyyy-MM-dd') {
if(t==''||t<=0||t==null||t=='null'){
return '';
}
if(typeof t == 'string'){
t = t.replace(new RegExp(/-/gm) ,"/");
}
const d = new Date(t);
const o = {
"M+" : d.getMonth() + 1,
// month
"d+" : d.getDate(),
// day
"h+" : d.getHours(),
// hour
"m+" : d.getMinutes(),
// minute
"s+" : d.getSeconds(),
// second
"q+" : Math.floor((d.getMonth() + 3) / 3),
// quarter
"S" : d.getMilliseconds()
// millisecond
};
if (/(y+)/.test(format) || /(Y+)/.test(format)) {
format = format.replace(RegExp.$1, (d.getFullYear() + "")
.substr(4 - RegExp.$1.length));
}
for ( let k in o) {
if (new RegExp("(" + k + ")").test(format)) {
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
: ("00" + o[k]).substr(("" + o[k]).length));
}
}
return format;
}
获取当前时间的上个月
export function getLastMonth() {//获取上个月日期
const date = new Date();
let year = date.getFullYear();
let month = date.getMonth();
if (month == 0) {
year = year - 1;
month = 12;
}
month = (month + "").length == 1 ? "0" + month : month
return year + "-" + month;
}
export function deepClone(source) {
if (!source && typeof source !== 'object') {
throw new Error('error arguments', 'deepClone')
}
const targetObj = source.constructor === Array ? [] : {}
Object.keys(source).forEach(keys => {
if (source[keys] && typeof source[keys] === 'object') {
targetObj[keys] = deepClone(source[keys])
} else {
targetObj[keys] = source[keys]
}
})
return targetObj
}
加
export function accAdd(arg1, arg2) {
let r1, r2, m, c;
try {
r1 = arg1.toString().split(".")[1].length;
}
catch (e) {
r1 = 0;
}
try {
r2 = arg2.toString().split(".")[1].length;
}
catch (e) {
r2 = 0;
}
c = Math.abs(r1 - r2);
m = Math.pow(10, Math.max(r1, r2));
if (c > 0) {
const cm = Math.pow(10, c);
if (r1 > r2) {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", "")) * cm;
} else {
arg1 = Number(arg1.toString().replace(".", "")) * cm;
arg2 = Number(arg2.toString().replace(".", ""));
}
} else {
arg1 = Number(arg1.toString().replace(".", ""));
arg2 = Number(arg2.toString().replace(".", ""));
}
return (arg1 + arg2) / m;
}
减
跟上面加法配合用
export function accSub(arg1, arg2) {
return accAdd(arg1, -arg2);
}
乘
export function accMul(arg1,arg2) {
let m=0,s1=arg1.toString(),s2=arg2.toString();
try{m+=s1.split(".")[1].length}catch(e){}
try{m+=s2.split(".")[1].length}catch(e){}
return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);
}
除
export function accDiv(arg1, arg2) {
let t1 = 0, t2 = 0, r1, r2;
try {
t1 = arg1.toString().split(".")[1].length;
} catch (e) {}
try {
t2 = arg2.toString().split(".")[1].length;
} catch (e) {}
r1 = Number(arg1.toString().replace(".", ""));
r2 = Number(arg2.toString().replace(".", ""));
return (r1 / r2) * Math.pow(10, t2 - t1);
}
重写原因:toFixed的四舍五入规则有问题,是四舍六入五成双,解决办法重写
Number.prototype.toFixed = function (n) {
if (n > 20 || n < 0) {
throw new RangeError('toFixed() digits argument must be between 0 and 20');
}
const number = this;
if (isNaN(number) || number >= Math.pow(10, 21)) {
return number.toString();
}
if (typeof (n) == 'undefined' || n == 0) {
return (Math.round(number)).toString();
}
// 判断是正数还是负数
let isPositive = true;
if(parseFloat(number)<0){
isPositive = false;
}
let result = number.toString();
// 增加判断科学计数法的支持(只处理绝对值小于1的小数科学技术法)
if(result.indexOf("e")>0&&Math.abs(number)<1){
result = scienceToString(number);
}
const arr = result.split('.');
// 整数的情况
if (arr.length < 2) {
result += '.';
for (let i = 0; i < n; i += 1) {
result += '0';
}
return result;
}
const integer = arr[0];
const decimal = arr[1];
if (decimal.length == n) {
return result;
}
if (decimal.length < n) {
for (let i = 0; i < n - decimal.length; i += 1) {
result += '0';
}
return result;
}
result = integer + '.' + decimal.substr(0, n);
const last = decimal.substr(n, 1);
// 四舍五入,转换为整数再处理,避免浮点数精度的损失
if (parseInt(last, 10) >= 5) {
const x = Math.pow(10, n);
if(isPositive){
result = (Math.round((parseFloat(result) * x)) + 1) / x;
} else {
result = (Math.round((parseFloat(result) * x)) - 1) / x;
}
result = result.toFixed(n);
}
return result;
};
//这个函数是先判断传入的是否是手机号
function isMobile(mobileNumber){
var reg = /^((1[3456789]{1}[0-9]{1})+\d{8})$/;
// var reg = /^[1][3,4,5,7,8][0-9]{9}$/;
return reg.test(mobileNumber);
}
//然后再隐藏手机号中间4位
function hideMobile(mobileNumber, mode) {
if(mode == 'visible'){
return mobileNumber;
}
if(!mobileNumber){return '';}//判断非空
if(isMobile(mobileNumber)){//判断是否是手机号
var reg = /^(\d{3})\d{4}(\d{4})$/;//手机加密
return mobileNumber.replace(reg, "$1****$2");
}else{
if(mobileNumber.length>=7){//固话加密
return mobileNumber.substr(0,mobileNumber.length-2)+"**";
}
return mobileNumber;
}
}
//身份证号加密
export function hideIdCard(idCardNumber, mode) {
if(mode == 'visible'){
return idCardNumber;
}
if(!idCardNumber){return '';}//判断非空
if(checkCertificateNo(idCardNumber)){//判断是否是身份证号
return idCardNumber.substring(0,3)+"****"; //身份证加密
}
}
//身份证号的验证
export function checkCertificateNo(num) {
let len = num.length, re;
if (len == 15)
re = new RegExp(/^([0-9]{6})()?([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{3})$/);
else if (len == 18)
re = new RegExp(/^([0-9]{6})()?([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{3})([0-9Xx])$/);
else {
//alert("请输入15或18位身份证号,您输入的是 "+len+ "位");
return false;
}
let a = num.match(re);
if (a != null) {
if (len == 15) {
var D = new Date("19" + a[3] + "/" + a[4] + "/" + a[5]);
var B = D.getYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
} else {
var D = new Date(a[3] + "/" + a[4] + "/" + a[5]);
var B = D.getFullYear() == a[3] && (D.getMonth() + 1) == a[4] && D.getDate() == a[5];
}
if (!B) {
//alert("输入的身份证号 "+ a[0] +" 里出生日期不对!");
return false;
}
} else {
return false;
}
// 验证最后一位的校验位
if(checkParity(num) === false)
{
return false;
}
return true;
}
//校验位的检测
const checkParity = function(obj)
{
//15位转18位
obj = changeFivteenToEighteen(obj);
var len = obj.length;
if(len == '18')
{
var arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
var cardTemp = 0, i, valnum;
for(i = 0; i < 17; i ++)
{
cardTemp += obj.substr(i, 1) * arrInt[i];
}
valnum = arrCh[cardTemp % 11];
if (valnum == obj.substr(17, 1))
{
return true;
}
return false;
}
return false;
};
//15位转18位身份证号
const changeFivteenToEighteen = function(obj)
{
if(obj.length == '15')
{
var arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
var cardTemp = 0, i;
obj = obj.substr(0, 6) + '19' + obj.substr(6, obj.length - 6);
for(i = 0; i < 17; i ++)
{
cardTemp += obj.substr(i, 1) * arrInt[i];
}
obj += arrCh[cardTemp % 11];
return obj;
}
return obj;
};
export function milliFormat(num) {
return num && num.toString()
.replace(/\d+/, function(s){
return s.replace(/(\d)(?=(\d{3})+$)/g, '$1,')
})
}
千位符格式化且可以保留两位小数
export function formatter(number) {
if(number) {
return String(number).replace(/\B(?=(\d{3})+(?!\d))/g, ',')
} else {
return '0.00'
}
}