Skip to content

zipObjectDeep

INFO

This function is only available in es-toolkit/compat for compatibility reasons. It either has alternative native JavaScript APIs or isn’t fully optimized yet.

When imported from es-toolkit/compat, it behaves exactly like lodash and provides the same functionalities, as detailed here.

Creates a deeply nested object given arrays of paths and values.

This function takes two arrays: one containing arrays of property paths, and the other containing corresponding values. It returns a new object where paths from the first array are used as key paths to set values, with corresponding elements from the second array as values. Paths can be dot-separated strings or arrays of property names. If the keys array is longer than the values array, the remaining keys will have undefined as their values.

Signature

typescript
function zipObjectDeep<P extends PropertyKey, V>(keys: ArrayLike<P | P[]>, values: ArrayLike<V>): { [K in P]: V };

Parameters

  • keys (ArrayLike<P | P[]>): An array of property names.
  • values (ArrayLike<V>): An array of values corresponding to the property names.

Returns

({ [K in P]: V }): A new object composed of the given property names and values.

Examples

typescript
import { zipObjectDeep } from 'es-toolkit/compat';

const paths = ['a.b.c', 'd.e.f'];
const values = [1, 2];
const result = zipObjectDeep(paths, values);
// result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }

const paths = [
  ['a', 'b', 'c'],
  ['d', 'e', 'f'],
];
const values = [1, 2];
const result = zipObjectDeep(paths, values);
// result will be { a: { b: { c: 1 } }, d: { e: { f: 2 } } }

const paths = ['a.b[0].c', 'a.b[1].d'];
const values = [1, 2];
const result = zipObjectDeep(paths, values);
// result will be { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }

Performance Comparison

Bundle SizePerformance
es-toolkit/compat938 bytes (88% smaller)1,102,767 times (25% slower)
lodash-es7,338 bytes1,476,660 times

Released under the MIT License.