Skip to content

pairsToObject ​

Input a object array pairs, and return a plain object composed of key-value pairs extracted from each sub-array. Optional parameters getKey and getValue can be provided to transform the array elements into keys and values. If not provided, the first element of each sub-array (index 0) will be used as the key, and the second element (index 1) will be used as the value.

getKey and getValue can be field paths of getByPath or callback functions.

Added in v0.0.1

Usage ​

ts
import { pairsToObject } from 'parsnip-kit'

const users = [['Alex', 16, 'vip'], ['Bob', 659, 'viewer'], ['Carter', 155, 'user'], ['Daniel', 825, 'user']]

pairsToObject(users)
// { Alex: 16, Bob: 659, Carter: 155, Daniel: 825 }

pairsToObject(users, '[0]', '[2]')
// { Alex: 'vip', Bob: 'viewer', Carter: 'user', Daniel: 'user' }

pairsToObject(users, pair => pair[0], pair => `${pair[1]} replies`)
// { Alex: '16 replies', Bob: '659 replies', Carter: '155 replies', Daniel: '825 replies' }

API ​

Type Parameter ​

ArgTypeDescription
Textends objectType of elements of array

Arguments ​

ArgTypeOptionalDefaultDescription
pairsT[]falseundefinedThe array of key-value object
getKeystring | ((item: T, index: number, arr: T[]) => any)trueundefinedExtract keys form sub-arrays
getValuestring | ((item: T, index: number, arr: T[]) => any)trueundefinedExtract values form sub-arrays

Returns ​

Type
ObjectLike

Reference ​

ObjectLike