js 数组、时间、邮箱等处理方法 阿星小栈

//判断数据是否在另一数组
util.inOf = function (arr, targetArr) {
    let res = true;
    arr.forEach(item => {
        if (targetArr.indexOf(item) < 0) {
            res = false;
        }
    });
    return res;
};
//判断元素是否在数组内
util.oneOf = function (ele, targetArr) {
    if (targetArr.indexOf(ele) >= 0) {
        return true;
    } else {
        return false;
    }
};
//判断元素是否在数组内 二
util.contains = function (arr, el) {
    if (!Array.indexOf) {
        Array.prototype.indexOf = function (obj) {
            for (var i = 0; i < this.length; i++) {
            if (this[i] == obj) {
                return i;
            }
            }
            return -1;
        }
    }
    if(arr.indexOf(el) < 0){
        return false;
    }else{
        console.log('arr.indexOf(obj)',arr.indexOf(el))
        return true;
    }
};
//获取窗口高度
util.getModalInitHeight = function () {
    return window.innerHeight;
}
//获取窗口宽度
util.innerWidth = function () {
    return window.innerWidth;
}
//转化日期格式为标准格式
util.convertTimeTextFormat = function (dateText) {
    let date = Date.parse(dateText);
    let datetime = new Date(date);
    let result = datetime.getFullYear() + '-' + ((datetime.getMonth() + 1) >= 10 ? (datetime.getMonth() + 1) : '0' +
            (datetime.getMonth() + 1)) + '-' + (datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate()) + ' ' +
            (datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours()) + ':' +
            (datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes()) +
            ':' + (datetime.getSeconds() < 10 ? '0' + datetime.getSeconds() : datetime.getSeconds());
    return result;
};
//转化多个时间为标准格式
util.convertTimeSlotFormat = function (timeslot) {
    let result = [];
    for(let i=0;i<timeslot.length;i++){
        if(timeslot[i]){
            let date = Date.parse(timeslot[i]);
            let datetime = new Date(date);
            let text = datetime.getFullYear() + '-' + ((datetime.getMonth() + 1) >= 10 ? (datetime.getMonth() + 1) : '0' +
                (datetime.getMonth() + 1)) + '-' + (datetime.getDate() < 10 ? '0' + datetime.getDate() : datetime.getDate()) + ' ' +
                (datetime.getHours() < 10 ? '0' + datetime.getHours() : datetime.getHours()) + ':' +
                (datetime.getMinutes() < 10 ? '0' + datetime.getMinutes() : datetime.getMinutes()) +
                ':' + (datetime.getSeconds() < 10 ? '0' + datetime.getSeconds() : datetime.getSeconds());
            if(text.length>0){
                result.push(text);
            } 
        }
    }
    return result;
};
//时间戳转化为自定义格式时间格式   (util.convertTimeTextCustomFormat('yyyy-MM-dd hh:mm:ss','2018-05-19'
util.formatTimestamp = function (fmt, timestamp) {
    let date = new Date(timestamp);
    let o = {   
        "M+" : date.getMonth()+1,                 //月份   
        "d+" : date.getDate(),                    //
        "h+" : date.getHours(),                   //小时   
        "m+" : date.getMinutes(),                 //
        "s+" : date.getSeconds(),                 //
        "q+" : Math.floor((date.getMonth()+3)/3), //季度   
        "S"  : date.getMilliseconds()             //毫秒   
      };   
      if(/(y+)/.test(fmt))   
        fmt=fmt.replace(RegExp.$1, (date.getFullYear()+"").substr(4 - RegExp.$1.length));   
      for(var k in o)   
        if(new RegExp("("+ k +")").test(fmt))   
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));   
      return fmt;
}
// 过滤字符、标签、空格及符号
util.newLine = function (str) {
    return str.replace(/
/g, '<br/>')
}
// 传入参数小于10,前面加'0'
util.checkNum = function (n) {
    return n = n < 10 ? '0' + n : n
}

// 获取url参数
util.getUrlParam = function (name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); // 构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); // 匹配目标参数
    if (r != null) {
        return unescape(r[2]);
    }
    return null; //返回参数值
}

// 判断是否IP
util.isIp = (ip) => {
    var reg = /^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$/
    return reg.test(ip);
}
//数组去重
util.unique = function (arr){
    var len = arr.length;
    var result = []
    for(var i=0;i<len;i++){
        var flag = true;
        for(var j = i;j<arr.length-1;j++){
            if(arr[i]==arr[j+1]){
                flag = false;
                break;
            }
        }
        if(flag){
            result.push(arr[i])
        }
    }
    return result;
}
//获取数组最大值或者最小值
util.getMaximin = function (arr, maximin) {
    if(maximin == "max"){
        return Math.max.apply(Math,arr);
    }else if(maximin == "min"){
        return Math.min.apply(Math, arr); 
    }
};
//邮箱验证
util.checkEmail = function (email) {
    let emailReg = new RegExp('^([0-9A-Za-z\-_\.]+)@([0-9a-z]+\.[a-z]{2,3}(\.[a-z]{2})?)$');
    if (email === '') {
        return false;
        console.log('不能为空');
    } else if (!emailReg.test(email)) { // 正则验证不通过,格式不对
        return false;
        console.log('格式不正确');
    } else {
        return true;
        console.log('正确');
    }
};
//数组分割成字符串用自定义符号连接
util.implode = function (glus, arr) {
    let str = '';
    for (let i = 0; i < arr.length; i++) {
        if (i === 0) {
            str += arr[i];
        } else {
            str += glus + arr[i];
        }
    }
    return str;
};

多个项目总结!!!!!!!!

原文地址:https://www.cnblogs.com/dereckbu/p/9024760.html