Skip to content

objectify

Convert an array to an object using specified key and value functions.

Syntax

typescript
objectify<T, K extends string | number, V>(
  array: T[],
  key: (item: T) => K,
  value?: (item: T) => V
): Record<K, V>

Parameters

  • array (T[]): The array to convert
  • key ((item: T) => K): Function to generate object keys
  • value ((item: T) => V, optional): Function to generate object values, defaults to returning the original item

Return Value

  • Record<K, V>: The converted object

Examples

Basic Usage

typescript
import { objectify } from 'radash'

const users = [
  { id: 1, name: 'Alice' },
  { id: 2, name: 'Bob' },
  { id: 3, name: 'Charlie' }
]

const usersById = objectify(users, user => user.id)
// {
//   1: { id: 1, name: 'Alice' },
//   2: { id: 2, name: 'Bob' },
//   3: { id: 3, name: 'Charlie' }
// }

Using Value Function

typescript
import { objectify } from 'radash'

const users = [
  { id: 1, name: 'Alice', age: 25 },
  { id: 2, name: 'Bob', age: 30 },
  { id: 3, name: 'Charlie', age: 35 }
]

const namesById = objectify(users, user => user.id, user => user.name)
// {
//   1: 'Alice',
//   2: 'Bob',
//   3: 'Charlie'
// }

Using String Keys

typescript
import { objectify } from 'radash'

const fruits = [
  { name: 'apple', color: 'red' },
  { name: 'banana', color: 'yellow' },
  { name: 'cherry', color: 'red' }
]

const colorsByName = objectify(fruits, fruit => fruit.name, fruit => fruit.color)
// {
//   apple: 'red',
//   banana: 'yellow',
//   cherry: 'red'
// }

Handling Duplicate Keys

typescript
import { objectify } from 'radash'

const items = [
  { category: 'fruit', name: 'apple' },
  { category: 'fruit', name: 'banana' },
  { category: 'vegetable', name: 'carrot' }
]

const itemsByCategory = objectify(items, item => item.category)
// {
//   fruit: { category: 'vegetable', name: 'carrot' }, // Only keeps the last one
//   vegetable: { category: 'vegetable', name: 'carrot' }
// }

Using Complex Keys

typescript
import { objectify } from 'radash'

const products = [
  { id: 1, category: 'electronics', name: 'Laptop' },
  { id: 2, category: 'electronics', name: 'Phone' },
  { id: 3, category: 'books', name: 'Novel' }
]

const productsByCategory = objectify(
  products,
  product => product.category,
  product => product.name
)
// {
//   electronics: 'Phone', // Only keeps the last one
//   books: 'Novel'
// }

Handling Nested Objects

typescript
import { objectify } from 'radash'

const orders = [
  { id: 1, customer: { id: 101, name: 'Alice' } },
  { id: 2, customer: { id: 102, name: 'Bob' } },
  { id: 3, customer: { id: 101, name: 'Alice' } }
]

const ordersByCustomerId = objectify(
  orders,
  order => order.customer.id,
  order => order.id
)
// {
//   101: 3, // Only keeps the last one
//   102: 2
// }

Using Number Keys

typescript
import { objectify } from 'radash'

const scores = [
  { player: 'Alice', score: 100 },
  { player: 'Bob', score: 85 },
  { player: 'Charlie', score: 120 }
]

const scoresByPlayer = objectify(scores, score => score.player, score => score.score)
// {
//   Alice: 100,
//   Bob: 85,
//   Charlie: 120
// }

Handling Empty Arrays

typescript
import { objectify } from 'radash'

const emptyArray: any[] = []
const result = objectify(emptyArray, item => item.id)
// {}

Using Computed Keys

typescript
import { objectify } from 'radash'

const events = [
  { title: 'Meeting 1', date: new Date('2023-01-01') },
  { title: 'Meeting 2', date: new Date('2023-01-02') },
  { title: 'Meeting 3', date: new Date('2023-01-01') }
]

const eventsByDate = objectify(
  events,
  event => event.date.toISOString().split('T')[0],
  event => event.title
)
// {
//   '2023-01-01': 'Meeting 3', // Only keeps the last one
//   '2023-01-02': 'Meeting 2'
// }

Using Conditional Values

typescript
import { objectify } from 'radash'

const users = [
  { id: 1, name: 'Alice', active: true },
  { id: 2, name: 'Bob', active: false },
  { id: 3, name: 'Charlie', active: true }
]

const statusByUser = objectify(
  users,
  user => user.name,
  user => user.active ? 'active' : 'inactive'
)
// {
//   Alice: 'active',
//   Bob: 'inactive',
//   Charlie: 'active'
// }

Handling Type Conversion

typescript
import { objectify } from 'radash'

const numbers = [1, 2, 3, 4, 5]

const squaresByNumber = objectify(
  numbers,
  num => num,
  num => num * num
)
// {
//   1: 1,
//   2: 4,
//   3: 9,
//   4: 16,
//   5: 25
// }

Using Boolean Keys

typescript
import { objectify } from 'radash'

const items = [
  { name: 'apple', available: true },
  { name: 'banana', available: true },
  { name: 'orange', available: false }
]

const itemsByAvailability = objectify(
  items,
  item => item.available,
  item => item.name
)
// {
//   true: 'banana', // Only keeps the last one
//   false: 'orange'
// }

Notes

  1. Duplicate keys: If there are duplicate keys, later values will override earlier ones
  2. Key types: Keys must be strings or numbers
  3. Empty arrays: Empty arrays return empty objects
  4. Type safety: Supports complete TypeScript type inference

Differences from Other Functions

  • objectify: Convert arrays to objects
  • groupBy: Group by conditions, returns object arrays
  • keyBy: Similar functionality, but may have different implementations

Performance

  • Time Complexity: O(n), where n is the array length
  • Space Complexity: O(n)
  • Use Cases: Data conversion, lookup optimization

Released under the MIT License.