简体   繁体   English

如何用 TypeScript 压平一棵树

[英]How to flatten a tree with TypeScript

I have this TypeScript code:我有这个 TypeScript 代码:

function flattenTree <T, K extends keyof T>(tree: T, key: K): T[] {
  const flattedTree = {
    ...tree,
  };
  delete flattedTree[key];

  if (Array.isArray(tree[key]) && (tree[key] as any as T[]).length > 0) {
    return [flattedTree].concat(
      (tree[key] as any as T[]).map(child => flattenTree(child, key))
        .reduce((pre, next) => pre.concat(next)),
    );
  }

  return [flattedTree];
}

Input:输入:

interface ShopTree {
  shopId: string;
  name: string;
  nodeType: string;
  children: ShopTree[];
}
const testObj: ShopTree = {
  shopId: '123',
  name: 'KFC(root)',
  nodeType: 'ROOT‘,
  children: [
    {
      shopId: '23',
      name: 'KFC(middle)',
      nodeType: 'MIDDLE',
      children: [{
        shopId: '45',
        name: 'KFC(leafA)',
        nodeType: 'LEAF’,
      }]
    },
    {
      shopId: '234',
      name: 'KFC(leafB)',
      nodeType: 'LEAF’,
    }
  ]
}

Output: Output:

console.log('result', flattenTree(testObj))

[
  {
    shopId: '123',
    name: 'KFC(root)',
    nodeType: 'ROOT‘,
  },
  {
    shopId: '23',
    name: 'KFC(middle)',
    nodeType: 'MIDDLE',
  },
  {
    shopId: '45',
    name: 'KFC(leafA)',
    nodeType: 'LEAF’,
  }
  ....
]

Could someone give me an idea of how to write the right types for the function?有人可以告诉我如何为 function 编写正确的类型吗?

While the output has no property value of children and I don't want to use as any as xxx in the code.虽然 output 没有children的属性值,但我不想在代码中as any as xxx

Could someone give me an idea of how to write the right types for the function?有人可以告诉我如何为 function 编写正确的类型吗?

While the output has no property value of children and I don't want to use as any as xxx in the code.虽然 output 没有children的属性值,但我不想在代码中as any as xxx

Without seeing how your tree is structured, it's a little difficult to provide any more precise help.如果不了解您的树的结构,提供任何更精确的帮助有点困难。 Here's an example of how the type might look:这是该类型的外观示例:

const flattenTree = <T>(tree: INode<T> | null): T[] => tree === null
    ? []
    : [tree.value, ...flattenTree(tree.left), ...flattenTree(tree.right)]

A flattened tree of <T> returns T[] <T>的扁平树返回T[]


Here's a full example of how that would flatten a tree:这是一个完整的示例,说明如何使树变平:

interface INode<T> {
    left: INode<T> | null;
    right: INode<T> | null;
    value: T;
}

const flattenTree = <T>(tree: INode<T> | null): T[] => tree === null
    ? []
    : [tree.value, ...flattenTree(tree.left), ...flattenTree(tree.right)]

const testTree: INode<number> = {
    left: {
        left: null,
        right: {
            left: null,
            right: {
                left: null,
                right: null,
                value: 7,
            },
            value: 5,
        },
        value: 3,
    },
    right: {
        right: {
            left: {
                left: null,
                right: null,
                value: 0,
            },
            right: null,
            value: 9,
        },
        left: {
            right: null,
            left: null,
            value: 1,
        },
        value: 10,
    },
    value: 2,
};

const result = flattenTree(testTree);
console.log(result); 
/* gives:
0: 2
1: 3
2: 5
3: 7
4: 10
5: 1
6: 9
7: 0
*/

Typescript playground link. Typescript 操场链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM