常用的函數(shù)封裝

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ù)...

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容