Skip to content

objectify

将数组转换为对象,使用指定的键和值函数。

语法

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

参数

  • array (T[]): 要转换的数组
  • key ((item: T) => K): 用于生成对象键的函数
  • value ((item: T) => V, 可选): 用于生成对象值的函数,默认为返回原项

返回值

  • Record<K, V>: 转换后的对象

示例

基本用法

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' }
// }

使用值函数

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'
// }

使用字符串键

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'
// }

处理重复键

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' }, // 只保留最后一个
//   vegetable: { category: 'vegetable', name: 'carrot' }
// }

使用复杂键

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', // 只保留最后一个
//   books: 'Novel'
// }

处理嵌套对象

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, // 只保留最后一个
//   102: 2
// }

使用数字键

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
// }

处理空数组

typescript
import { objectify } from 'radash'

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

使用计算键

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', // 只保留最后一个
//   '2023-01-02': 'Meeting 2'
// }

使用条件值

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'
// }

处理类型转换

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
// }

使用布尔值键

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', // 只保留最后一个
//   false: 'orange'
// }

注意事项

  1. 重复键: 如果有重复的键,后面的值会覆盖前面的值
  2. 键类型: 键必须是字符串或数字
  3. 空数组: 空数组会返回空对象
  4. 类型安全: 支持完整的 TypeScript 类型推断

与其他函数的区别

  • objectify: 将数组转换为对象
  • groupBy: 按条件分组,返回对象数组
  • keyBy: 类似功能,但可能有不同的实现

性能

  • 时间复杂度: O(n),其中 n 是数组长度
  • 空间复杂度: O(n)
  • 适用场景: 数据转换、查找优化

Released under the MIT License.