func-package

函数库, 面向复杂业务场景的 js 类库

transformArray(tree, childField)

将树结构转换成扁平数组

参数

tree: 树结构数组

childField: 每个节点中子树的字段名,默认值为 'children'。

Demo:

import { transformArray } from 'func-package';
const tree = [
{
id: '0',
name: '趣谈前端',
pid: '',
children: [
{
id: '0-1',
name: 'js',
pid: '0',
children: [],
},
{
id: '0-2',
name: 'css',
pid: '0',
children: [
{
id: '0-2-1',
name: 'css3',
pid: '0-2',
children: [],
},
],
},
],
},
];
console.log(transformArray(tree));
/*
[
{
id: '0',
name: '趣谈前端',
pid: '',
},
{
id: '0-1',
name: 'js',
pid: '0',
},
{
id: '0-2',
name: 'css',
pid: '0',
},
{
id: '0-2-1',
name: 'css3',
pid: '0-2',
},
];
/*