常用js函式的封裝集合,更新中...
普通氣泡排序
function bubbling(arr, isBigSmall) { // arr 陣列 , isBigSmall 是否從大到小排練 let num = arr.length for (let i = 0; i < num; i++) { for (let j = 0; j < num; j++) { if (arr[j] > arr[j + 1]) { let numNumber = arr[j] arr[j] = arr[j + 1] arr[j+ 1] = numNumber } } } if (isBigSmall || isBigSmall === true) { //從大到小排練 arr = arr.reverse() } return arr // 預設從小到大 }
快速排序
function rapidSort(arr, isBigSmall) { // arr 陣列 , isBigSmall 是否從大到小排練 let left = [] let right = [] let center = [] let arrLength= arr.length for (let i = 0; i < arrLength; i++) { if (arr[i] === arr[0]) { center.push(arr[i]) } else if (arr[i] > arr[0]) { right.push(arr[i]) } else { left.push(arr[i]) } }
//將陣列分為3部分, 同時進行排練,最後合併 let leftnew = bubblingSortList(left) let rightnew= bubblingSortList(right) let newArr = leftnew.concat(center, rightnew) if (isBigSmall || isBigSmall === true) { //從大到小排練 newArr = newArr.reverse() } return newArr }
function bubblingSortList(index) { let arrLength = index.length for (let i = 0; i < arrLength; i++) { for (let j = 0; j < arrLength; j++) { if (index[j] > index[j + 1]) { let num = index[j] index[j] = index[j + 1] index[j + 1] = num } } } return index }
常用的陣列去重ES5
function outRepetitionES5(arr) { let newArr = [] for (let i = 0; i < arr.length; i++) { if (newArr.indexOf(arr[i]) == -1) { newArr.push(arr[i]) } } return newArr }
陣列去重ES6
function outRepetitionES6(arr) { return [...new Set(arr)] }
獲取範圍隨機數
function randomFun(min, max, arrLength) { // min 隨機數最小範圍 ,ma隨機數最大範圍 arrLength[可選引數] 是否以陣列的形式返回多個 if (arrLength) { // 沒有長度返回單個隨機數 singleRandomFun(min, max) } else { // 返回一個指定長度的陣列,每一個都是隨機數 let arr = [] for (let i = 0; i < arrLength;i++) { arr.push(singleRandomFun(min, max)) } return arr } } //獲取單個隨機數 function singleRandomFun(min, max) { return Math.floor(Math.random() * (max - min)) }
獲取當前時間格式
function getDate(isTime, separatorFront,separatorDehind) { // isTime 是否需要詳細時間 separatorFront 設定指定分隔符(年月日的) 預設 "-" separatorDehind 設定指定分隔符(時間的) 預設':'
var t = new Date();
var n = t.getFullYear();
var y = bianhuan(t.getMonth() + 1);
var r = bianhuan(t.getDate());
var s = bianhuan(t.getHours());
var f = bianhuan(t.getMinutes());
var m = bianhuan(t.getSeconds());
let front = "-"
let behind = ':'
if (separatorFront) { // 是否自定義設定年月日的分隔符
front = separatorFront
}
if (separatorDehind) { // 是否自定義設定時間的分隔符
behind = separatorDehind
}
if (!isTime) {
var time = `${n}${front}${y}${front}${r}`
} else {
var time = `${n}${front}${y}${front} ${r} ${s}${behind}${f}${behind}${m}`
}
return time;
}
// 加“0”判斷
function bianhuan(s) {
s = s < 10 ? "0" + s : s;
return s;
}
判斷星期幾
function getWeek(getDate) { if (/^[0-9]{4}-[0-1]?[0-9]{1}-[0-3]?[0-9]{1}$/.test(getDate)) { // 判斷是日期格式 let str = '星期' let week = getDate.getDay() let strNumber = ['一', '二', '三', '四', '五', '六', '日'] return str + strNumber[week] } else { return '非日期格式' } }
生成指定位數的驗證碼
function specifiedYZM(numNumber, isLetter) { // 驗證碼長度[numNumber] 是否有字母 [isLetter] if (numNumber) { let arr1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] let arr2 = '1234567890qwertyuiopasdfghjklzxcvbnm' arr2 = arr2.split('') let arr = '' let arrLength = '' let tar = [] if (isLetter) { arr = arr2 arrLength = arr2.length } else { arr = arr1 arrLength = arr1.length } for (let i = 0; i < numNumber; i++) { tar.push(arr[Math.floor(Math.random() * arrLength)]) } tar = tar.join('') return tar } else { return '驗證碼長度不可為空' } }