js扁平化数组与tree转换
(取url参数+扁平化数组转tree部门+导入xlsx时间格式+递归tree扁平化)
点击查看代码
/**url取参数并以对象形式展示
* @param {string} url
* @returns {Object}
*/
export function param2Obj (url) {
const search = decodeURIComponent(url.split('?')[1]).replace(/\+/g, ' ')
if (!search) {
return {}
}
const obj = {}
const searchArr = search.split('&')
searchArr.forEach(v => {
const index = v.indexOf('=')
if (index !== -1) {
const name = v.substring(0, index)
const val = v.substring(index + 1, v.length)
obj[name] = val
}
})
return obj
}
/**
*
* @param {*} list list数组------》嵌套关系的数据
* { name: '', children: '' }------》树形结构
* 规律:
* 1. 公司的pid是-1
* 2. 公司下的顶级部门pid的值是''空字符串
* 3. 公司下的子部门的pid的值是父部门的id值
* 核心:找父部门
* 1. 新建转换的属性数据:[]
* 2. 构建映射关系 :map
* 3. 根据map的映射关系遍历list对比
* 4. 返回转换完的树形结构
*/
export function transformTreeData (list) {
console.log('要转换的数据:', list)
const treeData = []
const map = {}
list.forEach(item => {
map[item.id] = item
})
// console.log('映射关系:', map)
list.forEach(item => {
// 找父部门
// 排除pid=-1公司
if (item.pid === '-1') return
/**
* 核心:根据pid的值从map映射关系中找父部门
* 1. '' => 顶级部门
* 2. '父部门的id' =》
*/
// 如果存在则表示item不是最顶层的数据=>pid为父节点ID
const parent = map[item.pid]
if (parent) {
// 找到了
if (!parent.children) {
parent.children = []
}
parent.children.push(item)
} else {
// 没找到=》顶级(一级)部门
treeData.push(item)
}
// console.table(treeData)
})
return treeData
}
// excel时间格式化————————————————————————————————————————
/**
*excel时间要比正确时间多70年
* @param {*} numb excel时间格式 天数
* @param {*} format 转换分隔符
* @returns 标准时间格式
*/
export function formatExcelDate (numb, format) {
// 天数
const time = new Date((numb - 1) * 24 * 3600000 + 1)
console.log(time)
time.setYear(time.getFullYear() - 70)
const year = time.getFullYear() + ''
const month = time.getMonth() + 1 + ''
const date = time.getDate() + ''
// 转换的格式符号
if (format && format.length === 1) {
return year + format + month + format + date
}
return year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
}
//导入xlsx——————————————————————————————————————————————————
// 中文转英文
/**
* 把results中文转为英文属性
* results转为excel数据
*/
transformResults (results) {
/**
* 准备中英转化换的map
* 遍历results把读取的员工信息的中文属性根据map转换为英文属性
*/
// 根据excel中字段去定义
const userRelations = {
'入职日期': 'timeOfEntry',
'聘用形式': 'formOfEmployment',
'手机号': 'mobile',
'姓名': 'username',
'转正日期': 'correctionTime',
'工号': 'workNumber',
'部门': 'departmentName'
}
// 准备新数组存储转换完的结果
const newResults = []
results.forEach(user => {
// 存储转换成英文属性的用户信息
const newUser = {}
for (const key in user) {
const enKey = userRelations[key]
// 属性:key是中文的属性名====》转英文属性名enKey
// 时间格式化 入职+转正
if (enKey === 'timeOfEntry' || enKey === 'correctionTime') {
// excel时间转换(读取excel时间多70年)
newUser[enKey] = formatExcelDate(user[key], '-')
} else {
// enKey是英文
newUser[enKey] = user[key]
}
}
newResults.push(newUser)
})
return newResults
}
//路由——————————————————————————————————
function dataWith(routerList) {
const authMap = new Map()
// 添加首页
const newList = [
{
path: '/',
name: '首页',
},
...routerList,
]
// 递归处理数据
let resListMap = deepData(newList, authMap)
const obj = Object.fromEntries(resListMap)
return { obj, newList }
}
function deepData(newList, mapObj) {
newList.map((m) => {
mapObj.set(m.path, m)
if (m.children) {
deepData(m.children, mapObj)
}
})
return mapObj
}
本文来自博客园,作者:jialiangzai,转载请注明原文链接:https://www.cnblogs.com/zsnhweb/articles/17677688.html

浙公网安备 33010602011771号