数组扁平化
// 栗子
const arr = [1, [2, [3, 4]], 5, [6]];
// 方法01 - 递归
const flatten = (arr) => {
let res = [];
arr.map(item => {
if(Array.isArray(item)) {
res = res.concat(flatten(item));
} else {
res.push(item);
}
});
return res;
}
// 方法02 - reduce
const flatten = (arr) => {
return arr.reduce((result, item)=> {
return result.concat(Array.isArray(item) ? flatten(item) : item);
}, []);
}
// 方法03 - flat
const flatten = (arr) => {
return arr.flat(Infinity)
}
// 运行
const result = flatten(arr);
console.log(result);
// 运行结果
[1, 2, 3, 4, 5, 6]
模糊查询对象
// 栗子
const tree = [
{
name: '小明前端专家',
id: 1,
pid: 0,
children: [
{
name: '小花前端程序媛',
id: 11,
pid: 1,
children: [
{
name: '小华划水运动员',
id: 111,
pid: 11,
},
{
name: '小李摸鱼运动员',
id: 112,
pid: 11,
}
]
},
{
name: '小红摸鱼程序员',
id: 12,
pid: 1,
}
]
},
{
name: '小王内卷王',
id: 2,
pid: 0,
children: [
{
name: '小林摸鱼王',
id: 21,
pid: 2,
},
{
name: '小李后端程序员',
id: 22,
pid: 2,
}
]
}
]
// 方法
const fuzzyQueryTree = (arr, value) => {
if (!Array.isArray(arr) || arr.length === 0) {
return []
}
let result = [];
arr.forEach(item => {
if (item.name.indexOf(value) > -1) {
const children = fuzzyQueryTree(item.children, value);
const obj = { ...item, children }
result.push(obj);
} else {
if (item.children && item.children.length > 0) {
const children = fuzzyQueryTree(item.children, value);
const obj = { ...item, children }
if (children && children.length > 0) {
result.push(obj);
}
}
}
});
return result;
};
// 运行
const result = fuzzyQueryTree(tree,'程序');
console.log(result);
// 运行结果
[
{
"name": "小明前端专家",
"id": 1,
"pid": 0,
"children": [
{
"name": "小花前端程序媛",
"id": 11,
"pid": 1,
"children": []
},
{
"name": "小红摸鱼程序员",
"id": 12,
"pid": 1,
"children": []
}
]
},
{
"name": "小王内卷王",
"id": 2,
"pid": 0,
"children": [
{
"name": "小李后端程序员",
"id": 22,
"pid": 2,
"children": []
}
]
}
]
对象节点添加属性
// 栗子
const tree = [
{
name: '小明',
id: 1,
pid: 0,
children: [
{
name: '小花',
id: 11,
pid: 1,
children: [
{
name: '小华',
id: 111,
pid: 11,
},
{
name: '小李',
id: 112,
pid: 11,
}
]
},
{
name: '小红',
id: 12,
pid: 1,
}
]
},
{
name: '小王',
id: 2,
pid: 0,
children: [
{
name: '小林',
id: 21,
pid: 2,
},
{
name: '小李',
id: 22,
pid: 2,
}
]
}
]
// 方法
const addAttrToNodes = (tree) => {
tree.forEach((item) => {
item.title = '新生代农民工'
if (item.children && item.children.length > 0) {
addAttrToNodes(item.children)
}
})
return tree
}
// 运行
const result = addAttrToNodes(tree);
console.log(result);
// 运行结果
[
{
"name": "小明",
"id": 1,
"pid": 0,
"children": [
{
"name": "小花",
"id": 11,
"pid": 1,
"children": [
{
"name": "小华",
"id": 111,
"pid": 11,
"title": "新生代农民工"
},
{
"name": "小李",
"id": 112,
"pid": 11,
"title": "新生代农民工"
}
],
"title": "新生代农民工"
},
{
"name": "小红",
"id": 12,
"pid": 1,
"title": "新生代农民工"
}
],
"title": "新生代农民工"
},
{
"name": "小王",
"id": 2,
"pid": 0,
"children": [
{
"name": "小林",
"id": 21,
"pid": 2,
"title": "新生代农民工"
},
{
"name": "小李",
"id": 22,
"pid": 2,
"title": "新生代农民工"
}
],
"title": "新生代农民工"
}
]
对象节点删除属性
// 栗子
const tree = [
{
name: '小明',
id: 1,
pid: 0,
children: [
{
name: '小花',
id: 11,
pid: 1,
children: [
{
name: '小华',
id: 111,
pid: 11,
},
{
name: '小李',
id: 112,
pid: 11,
}
]
},
{
name: '小红',
id: 12,
pid: 1,
}
]
},
{
name: '小王',
id: 2,
pid: 0,
children: [
{
name: '小林',
id: 21,
pid: 2,
},
{
name: '小李',
id: 22,
pid: 2,
}
]
}
]
// 方法
const removeAttrFromNode = (tree) => {
tree.forEach((item) => {
delete item.title
if (item.children && item.children.length > 0) {
removeAttrFromNode(item.children)
}
})
return tree
}
// 运行
const result = removeAttrFromNode(tree);
console.log(result);
// 运行结果
[
{
"name": "小明",
"id": 1,
"pid": 0,
"children": [
{
"name": "小花",
"id": 11,
"pid": 1,
"children": [
{
"name": "小华",
"id": 111,
"pid": 11
},
{
"name": "小李",
"id": 112,
"pid": 11
}
]
},
{
"name": "小红",
"id": 12,
"pid": 1
}
]
},
{
"name": "小王",
"id": 2,
"pid": 0,
"children": [
{
"name": "小林",
"id": 21,
"pid": 2
},
{
"name": "小李",
"id": 22,
"pid": 2
}
]
}
]
删除对象中的空children
// 栗子
const tree = [
{
name: '小明',
id: 1,
pid: 0,
children: [
{
name: '小花',
id: 11,
pid: 1,
children: [
{
name: '小华',
id: 111,
pid: 11,
},
{
name: '小李',
id: 112,
pid: 11,
children: []
}
]
},
{
name: '小红',
id: 12,
pid: 1,
children: []
}
]
},
{
name: '小王',
id: 2,
pid: 0,
children: [
{
name: '小林',
id: 21,
pid: 2,
},
{
name: '小李',
id: 22,
pid: 2,
children: []
}
]
}
]
// 方法
const removeEmptyChildren = (tree) => {
tree.forEach((item) => {
if (item.children && item.children.length ===0) {
delete item.children
} else if (item.children && item.children.length > 0) {
removeEmptyChildren(item.children)
}
})
return tree
}
// 运行
const result = removeEmptyChildren(tree);
console.log(result);
// 运行结果
[
{
"name": "小明",
"id": 1,
"pid": 0,
"children": [
{
"name": "小花",
"id": 11,
"pid": 1,
"children": [
{
"name": "小华",
"id": 111,
"pid": 11
},
{
"name": "小李",
"id": 112,
"pid": 11
}
]
},
{
"name": "小红",
"id": 12,
"pid": 1
}
]
},
{
"name": "小王",
"id": 2,
"pid": 0,
"children": [
{
"name": "小林",
"id": 21,
"pid": 2
},
{
"name": "小李",
"id": 22,
"pid": 2
}
]
}
]
获取对象中所有的子节点
// 栗子
const tree = [
{
name: '小明',
id: 1,
pid: 0,
children: [
{
name: '小花',
id: 11,
pid: 1,
children: [
{
name: '小华',
id: 111,
pid: 11,
},
{
name: '小李',
id: 112,
pid: 11,
}
]
},
{
name: '小红',
id: 12,
pid: 1,
}
]
},
{
name: '小王',
id: 2,
pid: 0,
children: [
{
name: '小林',
id: 21,
pid: 2,
},
{
name: '小李',
id: 22,
pid: 2,
}
]
}
]
// 方法
const getAllLeaf = (tree) => {
const result = []
const getLeaf = (tree) => {
tree.forEach((item) => {
if (!item.children) {
result.push(item)
} else {
getLeaf(item.children)
}
})
}
getLeaf(tree)
return result
}
// 运行
const result = getAllLeaf(tree);
console.log(result);
// 运行结果
[
{
"name": "小华",
"id": 111,
"pid": 11
},
{
"name": "小李",
"id": 112,
"pid": 11
},
{
"name": "小红",
"id": 12,
"pid": 1
},
{
"name": "小林",
"id": 21,
"pid": 2
},
{
"name": "小李",
"id": 22,
"pid": 2
}
]