1. 遞歸 數(shù)組生成樹
/**
* 將數(shù)組轉(zhuǎn)為樹 遞歸
* @param { Array } list 傳入的數(shù)組
* @param { Number } pid 父級(jí)ID
* @return { Tree } 數(shù)據(jù)類型
*/
export function listToTree(list, pid = null) {
return list.filter(item => item.pid == pid).map(item => {
let children = listToTree(list, item.id)
if (children.length > 0) {
return { ...item, children }
} else {
return { ...item }
}
})
}
2. 字典表 數(shù)組生成樹
/**
* 講數(shù)組轉(zhuǎn)為樹 字典表
* @param { Array } inputValue
* @return { Tree } 數(shù)據(jù)類型
*/
export function toTree(inputValue) {
const newArr = []
const map = {}
inputValue.forEach(item => {
item.children = []
const key = item.id
map[key] = item
})
inputValue.forEach(item => {
const parent = map[item.pid]
if (parent) {
parent.children.push(item)
} else {
newArr.push(item)
}
})
return newArr
}
3. 遞歸刪除特定字段
/**
* 遞歸刪除特定字段
* @param { Tree } list
* @return { Tree } 數(shù)據(jù)類型
*/
export function resetTree(list, childrenName = 'children') {
for (let i = 0; i < list.length; i++) {
const item = list[i]
if (item && item[childrenName]) {
// children為空數(shù)組時(shí)刪除
if (item[childrenName].length === 0) {
delete item[childrenName]
} else {
// 遞歸當(dāng)前children數(shù)組
resetTree(item[childrenName], childrenName)
}
}
}
return list
}
4.判斷數(shù)據(jù)類型
/**
* 判斷數(shù)據(jù)類型
* @param { * } inputValue
* @return { string } 數(shù)據(jù)類型
*/
export function judgeDataType(inputValue) {
const typeString = Object.prototype.toString.call(inputValue)
return typeString.slice(8, -1)
}
5.保留兩位小數(shù)(不四舍五入)
// 保留兩位小數(shù)
export function retainDecimal(value1) {
return value1.replace(/([0-9]+\.[0-9]{2})[0-9]*/, '$1')
}
6.判斷設(shè)備類型
// 判斷當(dāng)前設(shè)備是H5還是pc
export function judgeDev() {
const userAgent = navigator.userAgent.toLowerCase()
return /ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(userAgent) ? 'H5' : 'PC'
}
7.復(fù)制文本
/**
* 點(diǎn)擊復(fù)制 兼容ios
* @param { string,number } text 需要復(fù)制的文本
*/
export function copy (text) {
// 數(shù)字沒有 .length 不能執(zhí)行selectText 需要轉(zhuǎn)化成字符串
const textString = text.toString()
let input = document.querySelector('#copy-input')
if (!input) {
input = document.createElement('input')
input.id = 'copy-input'
input.readOnly = 'readOnly' // 防止ios聚焦觸發(fā)鍵盤事件
input.style.position = 'absolute'
input.style.left = '-1000px'
input.style.zIndex = '-1000'
document.body.appendChild(input)
}
input.value = textString
// ios必須先選中文字且不支持 input.select();
selectText(input, 0, textString.length)
console.log(document.execCommand('copy'), 'execCommand')
if (document.execCommand('copy')) {
document.execCommand('copy')
console.log('已經(jīng)復(fù)制')
}
input.blur()
// input自帶的select()方法在蘋果端無法進(jìn)行選擇,所以需要自己去寫一個(gè)類似的方法
// 選擇文本。createTextRange(setSelectionRange)是input方法
function selectText(textbox, startIndex, stopIndex) {
if (textbox.createTextRange) {
// ie
const range = textbox.createTextRange()
range.collapse(true)
range.moveStart('character', startIndex) // 起始光標(biāo)
range.moveEnd('character', stopIndex - startIndex) // 結(jié)束光標(biāo)
range.select() // 不兼容蘋果
} else {
// firefox/chrome
textbox.setSelectionRange(startIndex, stopIndex)
textbox.focus()
}
}
}
未完待續(xù)...