Skip to content

objectToPairs

input an object obj, and return an array composed of arrays formed by each field's key-value pairs, or composed of the results returned by the optional parameter createItem.

Added in v0.0.1

Usage

ts
import { objectToPairs } from 'parsnip-kit'

const obj = {
 Alex: 16,
 Bob: 659,
 Carter: 155,
 Daniel: 825
}
objectToPairs(obj)
// [['Alex', 16], ['Bob', 659], ['Carter', 155], ['Daniel', 825]]

objectToPairs(obj, (value, key) => ({ [key]: value }))
// [{ Alex: 16 }, { Bob: 659 }, { Carter: 155 }, { Daniel: 825 }]

API

Type Parameter

ArgTypeDescription
Textends objectType of original object

Arguments

ArgTypeOptionalDefaultDescription
objTfalseundefinedOriginal object
createItem(value: T[string & keyof T], key: string, obj: T) => anytrueundefinedTo create element of array to be returned

Returns

Type
any[]