tool-array-methods
v1.0.4
Published
```js import { arrayTreeFlatten } from "tool-array-methods";
Downloads
9
Readme
arrayTreeFlatten 树的数组打平
import { arrayTreeFlatten } from "tool-array-methods";
let ids = 0;
const mockList = [
{
devId: ++ids,
address: `${Math.random()}`,
childrens: [
{
devId: ++ids,
address: `${Math.random()}`,
childrens: [
{
devId: ++ids,
address: `${Math.random()}`,
},
{
devId: ++ids,
address: `${Math.random()}`,
childrens: [
{
devId: ++ids,
address: `${Math.random()}`,
},
],
},
],
},
{
devId: ++ids,
address: `${Math.random()}`,
},
{
devId: ++ids,
address: `${Math.random()}`,
},
],
},
{
devId: ++ids,
address: `${Math.random()}`,
},
{
devId: ++ids,
address: `${Math.random()}`,
},
{
devId: ++ids,
address: `${Math.random()}`,
},
{
devId: ++ids,
address: `${Math.random()}`,
},
];
console.log(
arrayTreeFlatten(mockList, {
childKey: "childrens", // 默认 children
idKey: "devId", // 默认 id
itemFormat: (item) => {
// 返回 false 过滤数组中的元素
if (item.devId === 9) return false;
},
})
);