• 数组分组

    Example

    groupBy([{type: 1}, {type: 2}], "type") // returns {1: [{type: 1}], 2: [{type: 2}]}
    groupBy([{type: 1}, {value: 2}], "type") // returns {"*": [{value: 2}], 1: [{type: 1}]}

    Type Parameters

    • T extends {
          [k: string]: any;
      }

    • K extends string | number | symbol

    • R extends {
          [k: string]: T[];
      }

    Parameters

    • arr: T[]
    • key: K

      如果item中不存在该key,那么该item会归类到undefined

    • Optional defaultKey: string | number

      如果item中不存在该key,那么该item会归类到defaultKey

    Returns R

  • Type Parameters

    • T extends {
          [k: string]: any;
      }

    • R extends {
          [k: string]: T[];
      }

    Parameters

    • arr: T[]
    • by: ((it: T, result: any) => string | void)
        • (it: T, result: any): string | void
        • Parameters

          • it: T
          • result: any

          Returns string | void

    • Optional defaultKey: string | number

    Returns R