Skip to content

modeItem

Extract key values using the optional getter parameter (or directly using the array elements themselves), and return the first element that have 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 { modeItem } from 'parsnip-kit'

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

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

modeItem(users, (user) => user.name.toLowerCase()) // { id: 1, name: '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
T