1阿纤、數(shù)組套對(duì)象排序(根據(jù)對(duì)象內(nèi)的某個(gè)鍵進(jìn)行排序)
this.setArray(res.data.rows);//調(diào)用
// 排序
setArray(arr) {
arr.sort(compare('orderIndex'));//orderIndex 用作排序的鍵
function compare(keys) {
return function (a, b) {
return a[keys] - b[keys]
}
}
return arr
},
2、數(shù)組套對(duì)象去重(根據(jù)某個(gè)鍵值進(jìn)行去重)
var arr = [{from: '張三',to: '河南'},{from: '李四',to: '河北'},{from: '李四',to: '北京'},{from: '李四',to: '南京'},]
function setArr(arr) {
const res = new Map();
// form:需要根據(jù)哪個(gè)字段進(jìn)行去重
return arr.filter((a) => !res.has(a.from) && res.set(a.from, 1))
}
console.log(setArr(arr))
3夷陋、獲取當(dāng)前之前某天或者當(dāng)前之后某天(例:當(dāng)天是2022-08-04)
function getDay(day) {
var today = new Date();
var targetday_milliseconds = today.getTime() + 1000 * 60 * 60 * 24 * day;
today.setTime(targetday_milliseconds); //注意欠拾,這行是關(guān)鍵代碼
var tYear = today.getFullYear();
var tMonth = today.getMonth();
var tDate = today.getDate();
tMonth = doHandleTime(tMonth + 1);
tDate = doHandleTime(tDate);
return tYear + "-" + tMonth + "-" + tDate;
}
function doHandleTime(t) {
return t=t<10?'0'+t:t
}
console.log(getDay(-1))//2022-08-03
console.log(getDay(0))//2022-08-04
console.log(getDay(1))//2022-08-05
4胰锌、判斷當(dāng)前設(shè)備是移動(dòng)端還是PC端
// 是app?
this.isApp = navigator.userAgent.match(/(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i)
console.log(this.isApp ? '移動(dòng)端' : 'PC端');
5、省市區(qū)街道的JSON數(shù)據(jù)
6藐窄、解決H5上覆蓋一層遮罩層资昧,但是背景滾動(dòng)的問題
vue中:標(biāo)簽上添加 @touchmove.prevent
<div class="phoneDialog" @touchmove.prevent></div>
7、驗(yàn)證手機(jī)號(hào)和身份證號(hào)的格式
// 手機(jī)號(hào)校驗(yàn)
function phoneFun(params){
const phoneRule = /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
const regex = new RegExp(phoneRule);
return regex.test(params)
}
// 身份證校驗(yàn)
function cardIdFun(params){
const cardIdRule = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0[1-9]|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
const regex = new RegExp(cardIdRule);
return regex.test(params)
}
//使用
let mobile = 155632569856
console.log(phoneFun(mobile))
8荆忍、驗(yàn)證當(dāng)前是否是全屏狀態(tài)
function checkFull() {
//判斷瀏覽器是否處于全屏狀態(tài) (需要考慮兼容問題)
let isFull =
document.mozFullScreen ||
document.fullScreen ||
//谷歌瀏覽器及Webkit內(nèi)核瀏覽器
document.webkitIsFullScreen ||
document.webkitRequestFullScreen ||
document.mozRequestFullScreen ||
document.msFullscreenEnabled;
if (isFull === undefined) {
isFull = false;
}
return isFull;
}
9格带、從一個(gè)對(duì)象數(shù)組中取出想要的某一個(gè)鍵
let arr = [{a: 1,}, {b: 2,}, {c: 3}]
let keys = arr.reduce((sum, item) => ([...new Set([...sum, ...Object.keys(item)])]), [])//['a','b','c']
let res = arr[keys.indexOf('a')]
console.log(res)//{a:1}
//二:
var all = [
{ f1: "v1", f2: "v2" },
{ f1: "v1", f2: "v2", f3: "v3" },
{ f1: "v1", f2: "v2", f3: "v3", f4: "v4" },
{ f1: "v1", f2: "v2", f3: "v3", f4: "v4", fn: "vn" }
];
const output = Object.keys(Object.assign({}, ...all));
console.log(output);//['f1', 'f2', 'f3', 'f4', 'fn']