tool-array-methods
TypeScript icon, indicating that this package has built-in type declarations

1.0.4 • Public • Published

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;
    },
  })
);

Readme

Keywords

none

Package Sidebar

Install

npm i tool-array-methods

Weekly Downloads

0

Version

1.0.4

License

MIT

Unpacked Size

17.1 kB

Total Files

7

Last publish

Collaborators

  • vs1435