• js扩展方法(自用)


    //字符串转Date 字符串格式 yyyy-MM-dd HH:mm:ss
    String.prototype.toDate = function() {
    var date = eval('new Date(' + this.replace(/d+(?=-[^-]+$)/,
    function(a) {
    return parseInt(a, 10) - 1;
    }).match(/d+/g) + ')');
    return date;
    };

    //全部替换
    String.prototype.replaceAll = function(txt) {
    return this.replace(new RegExp(txt, "gm"), '');
    }

    //Date转字符串
    Date.prototype.toString = function(format) {
    var strYear = this.getFullYear(),
    strMonth = this.getMonth() + 1,
    strDay = this.getDate(),
    strHour = this.getHours(),
    strMinute = this.getMinutes(),
    strSecond = this.getSeconds();

    strMonth = strMonth < 10 ? '0' + strMonth : strMonth;
    strDay = strDay < 10 ? '0' + strDay : strDay;
    strHour = strHour < 10 ? '0' + strHour : strHour;
    strMinute = strMinute < 10 ? '0' + strMinute : strMinute;
    strSecond = strSecond < 10 ? '0' + strSecond : strSecond;

    if(!format || format.length == 0) {
    return strYear + "" + strMonth + "" + strDay + "" + strHour + "" + strMinute + "" + strSecond;
    }
    if(format.indexOf("yyyy") > -1) {
    format = format.replace("yyyy", strYear);
    }
    if(format.indexOf("MM") > -1) {
    format = format.replace("MM", strMonth);
    }
    if(format.indexOf("dd") > -1) {
    format = format.replace("dd", strDay);
    }
    if(format.indexOf("HH") > -1) {
    format = format.replace("HH", strHour);
    }
    if(format.indexOf("mm") > -1) {
    format = format.replace("mm", strMinute);
    }
    if(format.indexOf("ss") > -1) {
    format = format.replace("ss", strSecond);
    }
    return format;
    };
    //Date加年数
    Date.prototype.addYears = function(years) {
    var date = new Date(this.getTime());
    date.setFullYear(date.getFullYear() + years);
    return date;
    };
    //Date加月数
    Date.prototype.addMonths = function(months) {
    var date = new Date(this.getTime());
    date.setMonth(date.getMonth() + months);
    return date;
    };
    //Date加天数
    Date.prototype.addDays = function(days) {
    return new Date(this.getTime() + 1000 * 60 * 60 * 24 * days);
    };
    //Date加小时
    Date.prototype.addHours = function(hours) {
    return new Date(this.getTime() + 1000 * 60 * 60 * hours);
    };
    //Date加分钟
    Date.prototype.addMinutes = function(minutes) {
    return new Date(this.getTime() + 1000 * 60 * minutes);
    };
    //Date加秒
    Date.prototype.addSeconds = function(seconds) {
    return new Date(this.getTime() + 1000 * seconds);
    };

    //将数字改成千分位格式
    Number.prototype.toThousandType = function() {
    return(this + '').replace(/d{1,3}(?=(d{3})+(.d*)?$)/g, '$&,');
    };
    //数字格式化,如:00 000 0.00
    Number.prototype.format = function(format) {
    var str = this + "";
    if(!format || format.length < str.length) return str;
    var i = format.length - str.length;
    while(i-- > 0) {
    str = "0" + str;
    }
    return str;
    };

    //数组克隆
    Array.prototype.clone = function() {
    var s = [];
    for(var i = 0; i < this.length; i++) {
    var item = $.type(this[i]) == "object" ? $.extend({}, this[i]) : this[i];
    s.push(item);
    }
    return s;
    };
    //数组元素索引-数据元素是对象
    Array.prototype.indexOfObj = function(prop, element) {
    var result = -1;
    for(var i = 0; i < this.length; i++) {
    if(this[i][prop] == element) {
    result = i;
    break;
    }
    }
    return result;
    };
    //数组元素索引-数据元素不是对象
    Array.prototype.indexOf = function(element) {
    var result = -1;
    for(var i = 0; i < this.length; i++) {
    if(this[i] == element) {
    result = i;
    break;
    }
    }
    return result;
    };
    //数组删除元素-数据元素是对象
    Array.prototype.removeObj = function(prop, element) {
    var index = this.indexOfObj(prop, element);
    if(index < 0) return false;
    this.splice(index, 1);
    return true;
    };
    //数组删除元素-数据元素不是对象
    Array.prototype.remove = function(element) {
    var index = this.indexOf(element);
    if(index < 0) return false;
    this.splice(index, 1);
    return true;
    };
    //查找数组中符合条件的元素
    Array.prototype.where = function(condition) {
    var ret = [];
    for(var i = 0; i < this.length; i++) {
    if(condition(this[i])) {
    ret.push(this[i]);
    }
    }
    return ret;
    };
    //查找数组中第一个符合条件的元素
    Array.prototype.first = function(condition) {
    for(var i = 0; i < this.length; i++) {
    if(condition(this[i])) {
    return this[i];
    }
    }
    return undefined;
    }
    //判断数组是是否存在某元素
    Array.prototype.contains = function(condition) {
    return this.first(condition) != undefined;
    }
    //从数组中选择元素
    Array.prototype.select = function(condition) {
    var ret = [];
    for(var i = 0; i < this.length; i++) {
    ret.push(condition(this[i]));
    }
    return ret;
    }
    //向数组中追加数组
    Array.prototype.pushArray = function(arry) {
    for(var i = 0; i < arry.length; i++) {
    this.push(arry[i]);
    }
    }
    //从数组中选择前n个元素
    Array.prototype.top = function(count) {
    var ret = [];
    count = count < this.length ? count : this.length;
    for(var i = 0; i < count; i++) {
    ret.push(this[i]);
    }
    return ret;
    }
    Array.prototype.skip = function(count) {
    var ret = this.clone();
    count = count < this.length ? count : this.length;
    for(var i = 0; i < count; i++) {
    ret.splice(0, 1);
    }
    return ret;
    }
    //数组里对象根据某个字段进行排序
    Array.prototype.compare = function(solp) {
    function pai(property) {
    return function(obj1, obj2) {
    var value1 = obj1[property];
    var value2 = obj2[property];
    return value1 - value2;
    }
    }
    return this.sort(pai(solp));
    }
    //返回根据某一个字段和对应的值 查找出这个对象
    Array.prototype.indexforObj = function(prop, element) {
    var result = -1;
    for(var i = 0; i < this.length; i++) {
    if(this[i][prop] == element) {
    result = this[i];
    break;
    }
    }
    return result;
    };
    //数组取最大值
    Array.prototype.max = function() {
    // 将数组第一个元素的值赋给max
    var max = this[0];
    // 使用for 循环从数组第一个值开始做遍历
    for(var i = 1; i < this.length; i++) {
    // 如果元素当前值大于max,就把这个当前值赋值给max
    if(this[i] > max) {
    max = this[i];
    }
    }
    // 返回最大的值
    return max;
    }
    //图表rm单位值
    function formatUnit(str) {
    var result
    if(str == "doseRates") {
    result = {
    "xUnit": "S",
    "yUnit": "μSv/h"
    }
    } else if(str == "neutronDetectorRecords") {
    result = {
    "xUnit": "道址",
    "yUnit": "计数"
    }
    } else if(str == "gammaDetectorRecords") {
    result = {
    "xUnit": "道址",
    "yUnit": "计数"
    }
    } else if(str == "spectrumDataUnit") {
    result = {
    "xUnit": "能量(KeV)",
    "yUnit": "计数"
    }
    } else {
    result = {
    "xUnit": "S",
    "yUnit": "CPS"
    }
    }
    return result;
    };
    //设备报警率精确到小数6位
    function proportion(tatol, alarm) {
    var alarmText;
    if(alarm == 0) {
    alarmText = "0%"
    } else {
    var propor = tatol / alarm;
    switch(true) {
    case propor <= 100:
    alarmText = (alarm / tatol * 100).toFixed(2) + "%";
    break;
    case propor <= 1000 && propor > 100:
    alarmText = (alarm / tatol * 1000).toFixed(2) + "‰";
    break;
    case propor > 1000:
    alarmText = (alarm / tatol * 10000).toFixed(2) + "‱";
    }

    }
    return alarmText;
    };
    //根据rm的数组获取长度赋值给x轴
    function formatData(arr, key) {
    var obj = {};
    var arrx = [];
    if(key == 'gammaDetectorRecords') { // 伽马能谱
    for(var i = 0; i <= 1023; i++) {
    arrx.push(i)
    }
    obj.arrx = arrx;
    } else if(key == 'neutronDetectorRecords') { // 中子能谱
    for(var j = 0; j <= 15; j++) {
    arrx.push(j)
    }
    obj.arrx = arrx;
    } else {
    for(var q = 0; q <= arr.length; q++) {
    arrx.push(q)
    }
    obj.arrx = arrx;
    }
    // obj.arrY = arr;
    return obj.arrx
    };
    //图表设备为0的时候
    function noDev(obj) {
    let option = {
    title: obj.title,
    graphic: {
    type: 'text',
    left: 'center',
    top: 'center',
    z: 2,
    zlevel: 100,
    style: {
    text: "",
    fill: '#b5c7cd',
    font: 'bolder .20rem "Microsoft YaHei", sans-serif'
    }
    },
    series: [{
    name: '报警率',
    type: 'pie',
    radius: obj.radius,
    center: obj.center,
    avoidLabelOverlap: false,
    legendHoverLink: false,
    hoverAnimation: false,
    label: {
    normal: {
    show: false,
    },
    emphasis: {
    show: false

    }
    },
    labelLine: {
    normal: {
    show: false
    }
    },
    hoverOffset: 1,
    selectedOffset: 1,
    data: [{
    "name": "",
    "value": 0
    }],
    color: ["#b5c7cd"]
    }],
    }
    return option;
    }
    //提示框
    function message(vue, message, type) {
    vue.$notify({
    message: message,
    position: 'bottom-right',
    duration: 2000,
    type: type,
    customClass: type
    });
    }

    function find(str, cha, num) {
    var x = str.indexOf(cha);
    for(var i = 0; i < num; i++) { 
    x = str.indexOf(cha, x + 1); 
    }
    return x;
    }

    function clone(origin) {
    return Object.assign({}, origin);
    }

    本文来自博客园,作者:喆星高照,转载请注明原文链接:https://www.cnblogs.com/houxianzhou/p/15294378.html

  • 相关阅读:
    2015年北京大学软件project学科优秀大学生夏令营上机考试---C:单词翻转面试题
    跟我学Java多线程——线程池与堵塞队列
    Swift学习——类的定义,使用,继承,构造等(五)
    LNMP编译安装(centos7+nginx1.9+mysql5.6+php5.5)
    【iOS开发系列】九宫格布局
    出现异常时直接把e输出比输出e.getMessage()好得多
    往服务器上传个文件只要不到10毫秒,往数据库写条记录却要10秒
    使用struts的logic:iterate标签遍历列表时得到显示序号
    一次性上传多个文件到服务器端(一)
    Another MySQL daemon already running with the same unix socket的解决
  • 原文地址:https://www.cnblogs.com/houxianzhou/p/15294378.html
Copyright © 2020-2023  润新知