欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

utils.js使用案例详解

程序员文章站 2022-04-10 19:15:28
...
这次给大家带来utils.js使用案例详解,utils.js使用的注意事项有哪些,下面就是实战案例,一起来看一下。
//记录集中查找位置
    findIndex: function (id, feild, arr) {        let index = -1,
            tem = [];        if (id && feild && arr && arr.length) {
            arr.forEach((t, i) => {
                tem.push(t[feild]);
            });
            index = tem.indexOf(id);
        }        return index;
    },  
//日期格式化
    formatDate (date, fmt) {        var 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;
    },  
  //千分位处理
    thousands: function (num) {        if (typeof num !== 'number') return 0;        return (num.toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
    },  
  // 首字母大写
    capitalize: function (str) {        return str.substring(0,1).toUpperCase() + str.substring(1);
    },    // 处理简写数组
    calcArray: function (list, num, field, name) {        let arr = [];        if (list.length) {
            arr = list.map(item => {                return item[field];
            });
        }        if (arr.length > num) {            let s = '';            for(let i = 0; i < num; i++) {                if (i > 0) s += ',';
                s += arr[i];
            }            return s + '等' + arr.length + '个' + name;
        } else return arr.join(',');
    }

相信看了本文案例你已经掌握了方法,更多精彩请关注其它相关文章!

推荐阅读:

JS概念问题总结答疑

js基础提升学习之基本数据类型

以上就是utils.js使用案例详解的详细内容,更多请关注其它相关文章!