宜搭+
    正在准备搜索索引...

    类 CountMap<T>

    用于统计字符串键出现次数的计数映射。

    CountMap 继承自 Map,在键与计数器之间建立映射关系, 并可选择为每个键关联一个类型为 T 的对象。适用于频次统计、 标签计数、重复项检测等场景。

    26.4.13

    const map = CountMap.from([
    ["apple", { category: "fruit" }],
    "banana",
    ])
    map.count("apple")
    map.count("apple")
    console.log(map.getCount("apple")) // 2
    console.log(map.getObject("apple")) // { category: "fruit" }

    类型参数

    • T

      可选项关联对象的类型。

    层级

    • Map<string, { count: number; object?: T }>
      • CountMap
    索引

    构造函数

    • 类型参数

      • T

        可选项关联对象的类型。

      参数

      • 可选entries: readonly (readonly [string, { count: number; object?: T }])[] | null

      返回 CountMap<T>

    • 类型参数

      • T

        可选项关联对象的类型。

      参数

      • 可选iterable: Iterable<readonly [string, { count: number; object?: T }], any, any> | null

      返回 CountMap<T>

    属性

    "[toStringTag]": string
    size: number

    the number of elements in the Map.

    "[species]": MapConstructor

    方法

    • Returns an iterable of entries in the map.

      返回 MapIterator<[string, { count: number; object?: T }]>

    • Removes all elements from the Map.

      返回 void

    • 对指定键的计数器加一,并可关联一个对象。

      若键已存在,则将其计数加 1,并用传入的 object 覆盖已关联的对象; 若键不存在,则插入新记录,计数为 1

      参数

      • key: string

        需要计数的键。

      • 可选object: T

        可选的关联对象,将覆盖该键之前关联的对象。

      返回 void

      26.4.13

    • 参数

      • key: string

      返回 boolean

      true if an element in the Map existed and has been removed, or false if the element does not exist.

    • Returns an iterable of key, value pairs for every entry in the map.

      返回 MapIterator<[string, { count: number; object?: T }]>

    • Executes a provided function once per each key/value pair in the Map, in insertion order.

      参数

      • callbackfn: (
            value: { count: number; object?: T },
            key: string,
            map: Map<string, { count: number; object?: T }>,
        ) => void
      • 可选thisArg: any

      返回 void

    • Returns a specified element from the Map object. If the value that is associated to the provided key is an object, then you will get a reference to that object and any change made to that object will effectively modify it inside the Map.

      参数

      • key: string

      返回 { count: number; object?: T } | undefined

      Returns the element associated with the specified key. If no element is associated with the specified key, undefined is returned.

    • 获取指定键的当前计数值。

      若键不存在或尚未计数,则返回 0

      参数

      • key: string

        需要查询的键。

      返回 number

      该键的计数值;键不存在时返回 0

      26.4.13

    • 获取指定键关联的对象。

      若键不存在或未关联对象,则返回 undefined

      参数

      • key: string

        需要查询的键。

      返回 T | undefined

      该键关联的类型为 T 的对象,或 undefined

      26.4.13

    • 参数

      • key: string

      返回 boolean

      boolean indicating whether an element with the specified key exists or not.

    • Returns an iterable of keys in the map

      返回 MapIterator<string>

    • Adds a new element with a specified key and value to the Map. If an element with the same key already exists, the element will be updated.

      参数

      • key: string
      • value: { count: number; object?: T }

      返回 this

    • Returns an iterable of values in the map

      返回 MapIterator<{ count: number; object?: T }>

    • 从键数组或键-对象对数组创建新的 CountMap 实例。

      传入数组时,每个元素既可以是仅含键的字符串,也可以是 [键, 对象] 元组。 初始化后所有键的计数均为 0,后续可通过 CountMap.count 累加。

      类型参数

      • T

        可选项关联对象的类型。

      参数

      • keysOrEntries: string[] | [string, T][]

        键数组或键-对象对数组。

      返回 CountMap<T>

      已初始化的新 CountMap<T> 实例。

      26.4.13

      const map = CountMap.from<string>(["x", ["y", "obj"]])
      
    • Groups members of an iterable according to the return value of the passed callback.

      类型参数

      • K
      • T

      参数

      • items: Iterable<T>

        An iterable.

      • keySelector: (item: T, index: number) => K

        A callback which will be invoked for each item in items.

      返回 Map<K, T[]>