Skip to content

mode

Static Badge

Extract key values using the optional getter parameter (or directly using the array elements themselves), and return the most frequently occurring value.

The getter is a field path of getByPath or a callback function, used to provide a label for frequency statistics.

Added in v0.0.1

Usage

ts
import { mode } from 'parsnip-kit'

mode([1, 2, 2, 3, 3, 3]) // 3

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 1, name: 'alice' }
]
mode(users, 'id') // 1

mode(users, (user) => user.name.toLowerCase()) // 'alice'

API

Type Parameter

ArgTypeDescription
TType of input array

Arguments

ArgTypeOptionalDefaultDescription
dataT[]falseundefinedInput array
getterstring | ((item: T, index: number, arr: T[]) => any)trueundefinedProvide an identifier to distinguish the elements

Returns

Type
any