element表头筛选(根据后端的返回的数据)

//封装方法,data=>整个表格的数据,columns=>列数据
export const  filtrationMethod=(data,columns)=>{
    let arr = []
    let test = data.map(i => Object.entries(i))
    if(test.length !== 0){
      arr = test[0].map(i => [])
      let o = test[0].map(i => [])
      test.forEach((i, iIndex) => {
        i.forEach((j, jIndex) => {
            o[jIndex].push({text: j[1], value: j[1]})
            let obj = []
            obj[j[0]] = o[jIndex]
            arr[jIndex] = obj
        })
      })
      columns.forEach((i, index) => {
        arr.forEach((j, jIndex) => {
            if(i.FieldCode === Object.keys(j)[0]){
              // i['filtterArr'] = arr[jIndex][Object.keys(j)[0]] // 不去重
              i['filtterArr'] = uniqArrObject(arr[jIndex][Object.keys(j)[0]])  // 去重
            }
        })
      })
      return columns
    }else{
      return columns
    }
  }

  // 过滤去重
const uniqArrObject=(arr) => {
    let result = {}
    let finalResult = []
    for (let i = 0; i < arr.length; i++) {
      result[arr[i].text] = arr[i]
    }
    for (let key in result) {
      finalResult.push(result[key])
    }
    return finalResult
  }
posted @ 2021-03-30 17:06  seekHelp  阅读(249)  评论(0编辑  收藏  举报