Skip to content

objects

Variables

clone

const clone: typeof esToolkit_clone = esToolkit_clone;

Creates a shallow clone of the given object.

Template

The type of the object.

Param

The object to clone.

Returns

  • A shallow clone of the given object.

Examples

// Clone a primitive values
const num = 29;
const clonedNum = clone(num);
console.log(clonedNum); // 29
console.log(clonedNum === num); // true
// Clone an array
const arr = [1, 2, 3];
const clonedArr = clone(arr);
console.log(clonedArr); // [1, 2, 3]
console.log(clonedArr === arr); // false
// Clone an object
const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };
const clonedObj = clone(obj);
console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }
console.log(clonedObj === obj); // false

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


cloneDeep

const cloneDeep: typeof esToolkit_cloneDeep = esToolkit_cloneDeep;

Creates a deep clone of the given object.

Template

The type of the object.

Param

The object to clone.

Returns

  • A deep clone of the given object.

Examples

// Clone a primitive values
const num = 29;
const clonedNum = cloneDeep(num);
console.log(clonedNum); // 29
console.log(clonedNum === num); // true
// Clone an array
const arr = [1, 2, 3];
const clonedArr = cloneDeep(arr);
console.log(clonedArr); // [1, 2, 3]
console.log(clonedArr === arr); // false
// Clone an array with nested objects
const arr = [1, { a: 1 }, [1, 2, 3]];
const clonedArr = cloneDeep(arr);
arr[1].a = 2;
console.log(arr); // [1, { a: 2 }, [1, 2, 3]]
console.log(clonedArr); // [1, { a: 1 }, [1, 2, 3]]
console.log(clonedArr === arr); // false
// Clone an object
const obj = { a: 1, b: 'es-toolkit', c: [1, 2, 3] };
const clonedObj = cloneDeep(obj);
console.log(clonedObj); // { a: 1, b: 'es-toolkit', c: [1, 2, 3] }
console.log(clonedObj === obj); // false
// Clone an object with nested objects
const obj = { a: 1, b: { c: 1 } };
const clonedObj = cloneDeep(obj);
obj.b.c = 2;
console.log(obj); // { a: 1, b: { c: 2 } }
console.log(clonedObj); // { a: 1, b: { c: 1 } }
console.log(clonedObj === obj); // false

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


findKey

const findKey: typeof esToolkit_findKey = esToolkit_findKey;

Finds the key of the first element in the object that satisfies the provided testing function.

Param

The object to search.

Param

The function to execute on each value in the object. It takes three arguments: - value: The current value being processed in the object. - key: The key of the current value being processed in the object. - obj: The object that findKey was called upon.

Returns

The key of the first element in the object that passes the test, or undefined if no element passes.

Example

const users = {
  'barney':  { 'age': 36, 'active': true },
  'fred':    { 'age': 40, 'active': false },
  'pebbles': { 'age': 1,  'active': true }
};
findKey(users, function(o) { return o.age < 40; }); => 'barney'

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


flattenObject

const flattenObject: typeof esToolkit_flattenObject = esToolkit_flattenObject;

The delimiter to use between nested keys.

Default

'.'

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


invert

const invert: typeof esToolkit_invert = esToolkit_invert;

Inverts the keys and values of an object. The keys of the input object become the values of the output object and vice versa.

This function takes an object and creates a new object by inverting its keys and values. If the input object has duplicate values, the key of the last occurrence will be used as the value for the new key in the output object. It effectively creates a reverse mapping of the input object's key-value pairs.

Template

Type of the keys in the input object (string, number, symbol)

Template

Type of the values in the input object (string, number, symbol)

Param

The input object whose keys and values are to be inverted

Returns

  • A new object with keys and values inverted

Example

invert({ a: 1, b: 2, c: 3 }); // { 1: 'a', 2: 'b', 3: 'c' }
invert({ 1: 'a', 2: 'b', 3: 'c' }); // { a: '1', b: '2', c: '3' }
invert({ a: 1, 2: 'b', c: 3, 4: 'd' }); // { 1: 'a', b: '2', 3: 'c', d: '4' }
invert({ a: Symbol('sym1'), b: Symbol('sym2') }); // { [Symbol('sym1')]: 'a', [Symbol('sym2')]: 'b' }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


mapKeys

const mapKeys: typeof esToolkit_mapKeys = esToolkit_mapKeys;

Creates a new object with the same values as the given object, but with keys generated by running each own enumerable property of the object through the iteratee function.

Template

The type of the object.

Template

The type of the new keys generated by the iteratee function.

Param

The object to iterate over.

Param

The function invoked per own enumerable property.

Returns

  • Returns the new mapped object.

Example

// Example usage:
const obj = { a: 1, b: 2 };
const result = mapKeys(obj, (value, key) => key + value);
console.log(result); // { a1: 1, b2: 2 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


mapValues

const mapValues: typeof esToolkit_mapValues = esToolkit_mapValues;

Creates a new object with the same keys as the given object, but with values generated by running each own enumerable property of the object through the iteratee function.

Template

The type of the object.

Template

The type of the keys in the object.

Template

The type of the new values generated by the iteratee function.

Param

The object to iterate over.

Param

The function invoked per own enumerable property.

Returns

  • Returns the new mapped object.

Example

// Example usage:
const obj = { a: 1, b: 2 };
const result = mapValues(obj, (value) => value * 2);
console.log(result); // { a: 2, b: 4 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


merge

const merge: typeof esToolkit_merge = esToolkit_merge;

Merges the properties of the source object into the target object.

This function performs a deep merge, meaning nested objects and arrays are merged recursively. If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged. If a property in the source object is undefined, it will not overwrite a defined property in the target object.

Note that this function mutates the target object.

Param

The target object into which the source object properties will be merged. This object is modified in place.

Param

The source object whose properties will be merged into the target object.

Returns

The updated target object with properties from the source object merged in.

Template

Type of the target object.

Template

Type of the source object.

Examples

const target = { a: 1, b: { x: 1, y: 2 } };
const source = { b: { y: 3, z: 4 }, c: 5 };

const result = merge(target, source);
console.log(result);
// Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
const target = { a: [1, 2], b: { x: 1 } };
const source = { a: [3], b: { y: 2 } };

const result = merge(target, source);
console.log(result);
// Output: { a: [3, 2], b: { x: 1, y: 2 } }
const target = { a: null };
const source = { a: [1, 2, 3] };

const result = merge(target, source);
console.log(result);
// Output: { a: [1, 2, 3] }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


mergeWith

const mergeWith: typeof esToolkit_mergeWith = esToolkit_mergeWith;

Merges the properties of the source object into the target object.

You can provide a custom merge function to control how properties are merged. It should return the value to be set in the target object.

If it returns undefined, a default deep merge will be applied for arrays and objects:

  • If a property in the source object is an array or an object and the corresponding property in the target object is also an array or object, they will be merged.
  • If a property in the source object is undefined, it will not overwrite a defined property in the target object.

Note that this function mutates the target object.

Param

The target object into which the source object properties will be merged. This object is modified in place.

Param

The source object whose properties will be merged into the target object.

Param

A custom merge function that defines how properties should be combined. It receives the following arguments: - targetValue: The current value of the property in the target object. - sourceValue: The value of the property in the source object. - key: The key of the property being merged. - target: The target object. - source: The source object.

Returns

The updated target object with properties from the source object merged in.

Template

Type of the target object.

Template

Type of the source object.

Examples

const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };

mergeWith(target, source, (targetValue, sourceValue) => {
  if (typeof targetValue === 'number' && typeof sourceValue === 'number') {
    return targetValue + sourceValue;
  }
});
// Returns { a: 1, b: 5, c: 4 }
const target = { a: [1], b: [2] };
const source = { a: [3], b: [4] };

const result = mergeWith(target, source, (objValue, srcValue) => {
  if (Array.isArray(objValue)) {
    return objValue.concat(srcValue);
  }
});

expect(result).toEqual({ a: [1, 3], b: [2, 4] });

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


omit

const omit: typeof esToolkit_omit = esToolkit_omit;

Creates a new object with specified keys omitted.

This function takes an object and an array of keys, and returns a new object that excludes the properties corresponding to the specified keys.

Template

The type of object.

Template

The type of keys in object.

Param

The object to omit keys from.

Param

An array of keys to be omitted from the object.

Returns

A new object with the specified keys omitted.

Example

const obj = { a: 1, b: 2, c: 3 };
const result = omit(obj, ['b', 'c']);
// result will be { a: 1 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


omitBy

const omitBy: typeof esToolkit_omitBy = esToolkit_omitBy;

Creates a new object composed of the properties that do not satisfy the predicate function.

This function takes an object and a predicate function, and returns a new object that includes only the properties for which the predicate function returns false.

Template

The type of object.

Param

The object to omit properties from.

Param

A predicate function that determines whether a property should be omitted. It takes the property's key and value as arguments and returns true if the property should be omitted, and false otherwise.

Returns

A new object with the properties that do not satisfy the predicate function.

Example

const obj = { a: 1, b: 'omit', c: 3 };
const shouldOmit = (value) => typeof value === 'string';
const result = omitBy(obj, shouldOmit);
// result will be { a: 1, c: 3 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


pick

const pick: typeof esToolkit_pick = esToolkit_pick;

Creates a new object composed of the picked object properties.

This function takes an object and an array of keys, and returns a new object that includes only the properties corresponding to the specified keys.

Template

The type of object.

Template

The type of keys in object.

Param

The object to pick keys from.

Param

An array of keys to be picked from the object.

Returns

A new object with the specified keys picked.

Example

const obj = { a: 1, b: 2, c: 3 };
const result = pick(obj, ['a', 'c']);
// result will be { a: 1, c: 3 }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


pickBy

const pickBy: typeof esToolkit_pickBy = esToolkit_pickBy;

Creates a new object composed of the properties that satisfy the predicate function.

This function takes an object and a predicate function, and returns a new object that includes only the properties for which the predicate function returns true.

Template

The type of object.

Param

The object to pick properties from.

Param

A predicate function that determines whether a property should be picked. It takes the property's key and value as arguments and returns true if the property should be picked, and false otherwise.

Returns

A new object with the properties that satisfy the predicate function.

Example

const obj = { a: 1, b: 'pick', c: 3 };
const shouldPick = (value) => typeof value === 'string';
const result = pickBy(obj, shouldPick);
// result will be { b: 'pick' }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit


toMerged

const toMerged: typeof esToolkit_toMerged = esToolkit_toMerged;

Merges the properties of the source object into a deep clone of the target object. Unlike merge, This function does not modify the original target object.

This function performs a deep merge, meaning nested objects and arrays are merged recursively.

  • If a property in the source object is an array or object and the corresponding property in the target object is also an array or object, they will be merged.
  • If a property in the source object is undefined, it will not overwrite a defined property in the target object.

Note that this function does not mutate the target object.

Param

The target object to be cloned and merged into. This object is not modified directly.

Param

The source object whose properties will be merged into the cloned target object.

Returns

A new object with properties from the source object merged into a deep clone of the target object.

Template

Type of the target object.

Template

Type of the source object.

Examples

const target = { a: 1, b: { x: 1, y: 2 } };
const source = { b: { y: 3, z: 4 }, c: 5 };

const result = toMerged(target, source);
console.log(result);
// Output: { a: 1, b: { x: 1, y: 3, z: 4 }, c: 5 }
const target = { a: [1, 2], b: { x: 1 } };
const source = { a: [3], b: { y: 2 } };

const result = toMerged(target, source);
console.log(result);
// Output: { a: [3, 2], b: { x: 1, y: 2 } }
const target = { a: null };
const source = { a: [1, 2, 3] };

const result = toMerged(target, source);
console.log(result);
// Output: { a: [1, 2, 3] }

See

Powered by es-toolkit (MIT License): https://github.com/toss/es-toolkit