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'
// }
注意事項
- 重復鍵: 如果有重復的鍵,後面的值會覆蓋前面的值
- 鍵類型: 鍵必須是字符串或數字
- 空數組: 空數組會返回空對象
- 類型安全: 支持完整的 TypeScript 類型推斷
與其他函數的區別
objectify
: 將數組轉換為對象groupBy
: 按條件分組,返回對象數組keyBy
: 類似功能,但可能有不同的實現
性能
- 時間復雜度: O(n),其中 n 是數組長度
- 空間復雜度: O(n)
- 適用場景: 數據轉換、查找優化