Skip to content

zipToObject

输入两个数组keysvalues,返回keys元素作为键,values元素作为值的普通对象。

可选参数getKeygetValue用于把对象元素转换为键和值,它们是getByPath函数的字段路径,或者回调函数。

Added in v0.0.1

Usage

ts
import { zipToObject } from 'parsnip-kit'

zipToObject(['id', 'name', 'skill'], [1, 'Alex', ['Javascript']])
// { id: 1, name: 'Alex', skill: ['Javascript'] }

const users = [{ id: 0, user: 'IAmBot' }, { id: 2, user: 'Alice' }, { id: 5, user: 'Tom' }]
const record = [
  { system: 'Linux', count: 99999, userId: 0 },
  { system: 'Mac OS', count: 10, userId: 2 },
  { system: 'Window', count: 2, userId: 5 },
]
zipToObject(
  users, record, 'user', 'count'
) // { IAmBot: 99999, Alice: 10, Tom: 2 }

zipToObject(
  users, record, item => item.user, item => item.count
) // { IAmBot: 99999, Alice: 10, Tom: 2 }

API

Type Parameter

ArgTypeDescription
T作为键的数组元素类型
U作为值的数组元素类型

Arguments

ArgTypeOptionalDefaultDescription
keysT[]falseundefined作为键的数组
valuesU[]falseundefined作为值的数组
getKeystring | ((item: T, index: number, arr: T[]) => any)trueundefined把数组元素转换为键
getValuestring | ((item: U, index: number, arr: U[]) => any)trueundefined把数组元素转换为值

Returns

Type
ObjectLike

Reference

ObjectLike