Commit d399210e by Jings

add: 公共方法添加

parent 371e935f
......@@ -542,4 +542,71 @@ export function initDataRange() {
let startDay = startDate.getDate() < 10 ? '0' + startDate.getDate() : startDate.getDate();
let startNewMonth = startMonth < 10 ? '0' + startMonth : startMonth;
return [`${startYear}-${startNewMonth}-${startDay}`, `${year}-${newMonth}-${day}`];
}
/**
* 将平铺的数据结构转为树形结构
* @param {*} list 数组
* @param {*} rootValue 根元素id
* @param {*} param2 可配置参数
* @returns
*/
export function arrToTree(list,rootValue,{idName = 'id',parentIdName = 'parentId',childName = 'children'}= {}) {
const objMap ={}; // 暂存数组以id为key的映射关系
const result = []; // 结果
for (const item of list) {
const id = item[idName];
const parentId = item[parentIdName];
// 该元素有可能已经放入map中(找不到该项的parent时会先放入map)
objMap[id] = !objMap[id] ? item : {...item,...objMap[id]}
const treeItem = objMap[id]; // 找到映射的那一项 引用
if(parentId === rootValue) {
// 根元素 将结果放入
result.push(treeItem)
} else {
// 若父元素不存在,初始化父元素
if(!objMap[parentId]) {
objMap[parentId] = {}
}
// 若无该跟元素则放入map中
if(!objMap[parentId][childName]) {
objMap[parentId][childName] = []
}
objMap[parentId][childName].push(treeItem)
}
}
return result
}
export function getArrDifference(arr1,arr2) {
let result = [];
arr1.forEach(item => {
if(arr1.includes(item) && !arr2.includes(item)) {
result.push(item)
}
})
return result;
}
// 过滤删除和新增
export function getDelOrAddDate(arr1,arr2) {
var arr = [];
var bool = false;
for(var i=0;i<arr1.length;i++){
for(var j=0;j<arr2.length;j++){
//进行优化遇到相同直接跳出循环 同时支持对象比对
if(JSON.stringify(arr1[i])===JSON.stringify(arr2[j])){
bool = false;
break;
}else{
bool=i;
}
}
if(bool!==false)arr.push(arr1[bool]);
}
return arr;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment