这次给大家带来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(',');
}相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
良精商城网店购物系统是一套能够适合不同类型商品、超强灵活的多功能在线商店系统,三级分销 PC+移动端+微网站,为您提供了一个完整的在线开店解决方案。良精网店购物系统除了拥有一般网上商店系统所具有的所有功能,还拥有着其它网店系统没有的许多超强功能。多种独创的技术使得系统能满足各行业广大用户的各种各样的需求,是一个经过完善设计并适用于各种服务器环境的高效、全新、快速和优秀的网上购物软件解决方案。









