Home > Web Front-end > JS Tutorial > body text

Detailed explanation of utils.js use cases

php中世界最好的语言
Release: 2018-06-04 14:22:32
Original
3167 people have browsed it

这次给大家带来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 = &#39;&#39;;            for(let i = 0; i < num; i++) {                if (i > 0) s += &#39;,&#39;;
                s += arr[i];
            }            return s + &#39;等&#39; + arr.length + &#39;个&#39; + name;
        } else return arr.join(&#39;,&#39;);
    }
Copy after login

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

推荐阅读:

JS概念问题总结答疑

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

The above is the detailed content of Detailed explanation of utils.js use cases. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!