一行代碼去重數(shù)組
const list = [1, 1, 2, 3, 6, 45, 8, 5, 4, 6, 5]
const uniqueList = [...new Set(list)]
// [1, 2, 3, 6, 45, 8, 5, 4]
時間格式化
const dateFormatter = (formatter, date) => {
date = (date ? new Date(date) : new Date)
const Y = date.getFullYear() + '',
M = date.getMonth() + 1,
D = date.getDate(),
H = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds()
return formatter.replace(/YYYY|yyyy/g, Y) .replace(/YY|yy/g, Y.substr(2, 2))
.replace(/MM/g, (M < 10 ? '0' : '') + M)
.replace(/DD/g, (D < 10 ? '0' : '') + D)
.replace(/HH|hh/g, (H < 10 ? '0' : '') + H)
.replace(/mm/g, (m < 10 ? '0' : '') + m)
.replace(/ss/g, (s < 10 ? '0' : '') + s) }
dateFormatter('YYYY-MM-DD HH:mm', '1995/02/15 13:55')
// 1995-02-15 13:55
對象去重
// let tagMArry=JSON.parse(localStorage.getItem("g_tagMArry"));
// tagMArry.push(to.meta);
// let hash = {};
?// const newArr = tagMArry.reduceRight((item, next) => {
? ? ? //? ? hash[next.name] ? '' : hash[next.name] = true && item.push(next);
? ? ? //? ? return item
? // }, []);
判斷數(shù)組對象是否存在某個對象
var cun = tagMArry.some(item=>{
? ? ? ? if(item.name==to.meta.name){
? ? ? ? ? return true
? ? ? ? }
?})
RGB色值生成16進制色值
const rgb2Hex = rgb => {
let rgbList = rgb.toString().match(/\d+/g)
let hex = '#'
for (let i = 0,len = rgbList.length; i < len; ++i) {
????hex += ('0' + Number(rgbList[i]).toString(16)).slice(-2) }
????return hex
};
rgb2Hex('100, 50, 0')
// '#643200'