export default {
arrayToTree(list: any[], field = { id: 'id', pid: 'parentId', children: 'children' }) {
const result = [];
const { id, pid, children } = field;
const nodeMap = new Map();
for (const node of list) {
node[children] = node[children] || [];
nodeMap.set(node[id], node);
}
for (const node of list) {
const parent = nodeMap.get(node[pid]);
(parent ? parent.children : result).push(node);
}
return result;
},
//数组包含,mainArr是否包含containsArray
arrayContains(mainArr: any[], containsArray: any[]) {
if (mainArr.length < 1 && containsArray.length < 1) {
return true;
}
if (containsArray.length < 1) {
return false;
}
for (const i of containsArray) {
const data = mainArr.find(v => {
return JSON.stringify(v) === JSON.stringify(i);
});
if (!data) {
return false;
}
}
return true;
},
//返回首字母大写的类型 如Array,Object,String
typeJudgment(needJudgedData) {
const typeData = Object.prototype.toString.call(needJudgedData);
const types = typeData.slice(8, -1);
return types;
},
getQueryString(name) {
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
const r = window.location.search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
}
};
06-24
1724
08-02
318