-
-
Notifications
You must be signed in to change notification settings - Fork 235
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
树形结构转成列表 #40
Comments
const data = [
{
id: '1',
name: '父节点1',
children: [
{
id: '1-1',
name: '子节点1-1',
children: [
{
id: '1-1-1',
name: '子节点1-1-1'
},
{
id: '1-1-2',
name: '子节点1-1-2'
}
]
}
]
},
{
id: '2',
name: '父节点2',
children: [
{
id: '2-1',
name: '子节点2-1'
}
]
}
]
const result = data.reduce(function (acc, cur) {
acc.push({
id: cur.id,
name: cur.name,
parentId: cur.parentId
});
cur.children && cur.children.forEach(child => {
child.parentId = cur.id;
arguments.callee(acc, child);
})
return acc;
}, []); |
// 我更喜欢这种,因为在真是业务场景中,应该保留原始解构 |
interface DataItem {
id: number;
text: string;
parentId: number;
children?: DataItem[];
}
function treeToList(data: DataItem[]) {
const res: DataItem[] = [];
for (const item of data) {
dfs(item, res);
}
/**
* 该函数用于深度遍历第一个参数tree, 并格式化收集到第二个参数res
* @param tree
* @param res
*/
function dfs(tree: DataItem, res: DataItem[]): void {
const newTree = {
id: tree.id,
text: tree.text,
parentId: tree.parentId,
};
res.push(newTree);
if (tree.children) {
for (const item of tree.children) {
dfs(item, res);
}
}
}
return res;
} |
const data = [
{
id: '1',
name: '父节点1',
children: [
{
id: '1-1',
name: '子节点1-1',
children: [
{
id: '1-1-1',
name: '子节点1-1-1'
},
{
id: '1-1-2',
name: '子节点1-1-2'
}
]
}
]
},
{
id: '2',
name: '父节点2',
children: [
{
id: '2-1',
name: '子节点2-1'
}
]
}
]
function tree2list(treeList) {
const list = []
const traverse = (treeNode, callback) => {
const queue = [treeNode]
while (queue.length > 0) {
let node = queue.shift()
let newNode = {
id: node.id,
name: node.name
}
callback && callback(newNode)
if (node.children) {
queue.push(...node.children)
}
}
}
treeList.forEach(rootNode => {
traverse(rootNode, (node) => {
list.push(node)
})
})
return list
}
const list = tree2list(data)
console.log('list', list)
|
function toList(arr) {
return arr.reduce((prev, current, index) => {
return prev.concat(current, ...( current.children && current.children.length > 0 ? toList(current.children) : [] ))
}, [])
} |
function treeToList(root) {
const list = [];
if (!root) {
return list;
}
list.push({ name: root.name });
for (const child of root.children) {
list.push(...treeToList(child));
}
return list;
} |
const data = [
{
id: '1',
name: '父节点1',
children: [
{
id: '1-1',
name: '子节点1-1',
children: [
{
id: '1-1-1',
name: '子节点1-1-1'
},
{
id: '1-1-2',
name: '子节点1-1-2'
}
]
}
]
},
{
id: '2',
name: '父节点2',
children: [
{
id: '2-1',
name: '子节点2-1'
}
]
}
]
function treeToList(data) {
let result = []
data.forEach(item => {
result.push(item)
if (item.children) {
result = result.concat(treeToList(item.children))
delete item.children
}
})
return result
}
console.log(treeToList(data));
// 无副作用
function treeToList_2(data) {
let result = []
data.forEach(item => {
let obj = {
id: item.id,
name: item.name
}
result.push(obj)
if (item.children) {
result = result.concat(treeToList_2(item.children))
}
})
return result
}
console.log(treeToList_2(data)); |
function tree2list(data) {
let res = [],
path = {}
traversal(data)
return res
function traversal(node) {
if (!node) return
path.id = node.id
path.text = node.text
path.parentId = node.parentId
res.push({...path})
node.children && node.children.forEach(item => {
traversal(item)
})
delete path.id
delete node.text
delete node.parentId
}
} |
我这个可以不
|
第一行代码判断数组和数组长度的应该是或而不是与? |
function treeToList(data) { |
const data = [
{
id: '1',
name: '父节点1',
children: [
{
id: '1-1',
name: '子节点1-1',
children: [
{
id: '1-1-1',
name: '子节点1-1-1'
},
{
id: '1-1-2',
name: '子节点1-1-2'
}
]
}
]
},
{
id: '2',
name: '父节点2',
children: [
{
id: '2-1',
name: '子节点2-1'
}
]
}
]
const formatt = (data) => {
const res = []
const formatTree = (cur, parentId = 0) => {
const obj = {
id: cur.id,
name: cur.name,
pid: parentId
}
res.push(obj)
if(cur.children && cur.children.length) {
for(const c of cur.children) {
formatTree(c, cur.id)
}
}
return obj
}
for(const d of data) {
formatTree(d)
}
return res
}
console.log(formatt(data)); |
function flattenTree(tree, result = []) {
for (const node of tree) {
const { children, ...rest } = node;
console.log(rest);
result.push(rest);
if (children && children.length > 0) {
flattenTree(children, result);
}
}
return result;
}
const data = [
{
id: 1,
text: "节点1",
parentId: 0,
children: [
{
id: 2,
text: "节点1_1",
parentId: 1,
},
],
},
];
const flatData = flattenTree(data);
console.log(flatData); |
const tree2list = tree => {
const res = []
const dfs = data => {
for (const item of data) {
const children = item.children
Reflect.deleteProperty(item, 'children')
res.push(item)
if (Array.isArray(children) && children.length) {
dfs(children)
}
}
}
dfs(tree)
return res
} |
function treeToList(treeData, result = []) {
treeData.forEach(item => {
if (item.children) {
treeToList(item.children, result)
delete item.children
}
result.push(item)
})
return result
} |
const treeToListReduce = (data) =>
data.reduce((reducedArr, { children, ...otherItems }) => {
const retArr = [...reducedArr, otherItems]
return Array.isArray(children)
? retArr.concat(treeToListReduce(children))
: retArr;
}, []) |
// 树转列表
const data = [
{
id: 1,
text: '节点1',
parentId: 0,
children: [
{
id: 2,
text: '节点1_1',
parentId: 1
}
]
}
]
function treeToList(data) {
let reuslt = [];
for (const item of data) {
const children = item.children
if(children){
reuslt = reuslt.concat(treeToList(children))
}
const newItem = {...item}
delete newItem.children
reuslt.push(newItem)
}
return reuslt;
} |
const data = [ const treeToList = (tree) => { |
The text was updated successfully, but these errors were encountered: