Skip to main content

Object Methods

assign

Copy the values of all of the enumerable own properties from one or more source objects to a target object. Returns the target object.

Since: 0.1.0

Arguments

ParamTypeDescription
targetobjectThe target object to copy to.
sources...objectThe source object from which to copy properties.

Returns

TypeDescription
anyReturns the merged target object.

Declaration

declare const assign: {
<T extends {}, U>(target: T, source: U): T & U;
<T_1 extends {}, U_1, V>(
target: T_1,
source1: U_1,
source2: V
): T_1 & U_1 & V;
<T_2 extends {}, U_2, V_1, W>(
target: T_2,
source1: U_2,
source2: V_1,
source3: W
): T_2 & U_2 & V_1 & W;
(target: object, ...sources: any[]): any;
};

Examples

assign({}, { x: 1 }, { y: 10 }); // { x: 1, y: 10 }

assignIn

This method is like assign except that it iterates over own and inherited source properties.

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to merged
sources...anyThe source objects

Returns

TypeDescription
anyReturns the merged target object.

Declaration

declare function assignIn<T extends object, U>(object: T, source: U): T & U;
declare function assignIn<T extends object, U, V>(
object: T,
source1: U,
source2: V
): T & U & V;
declare function assignIn<T extends object, U, V, W>(
object: T,
source1: U,
source2: V,
source3: W
): T & U & V & W;
declare function assignIn(object: any, ...sources: any[]): any;

Examples

function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;

assignIn({ x: 'x' }, new Foo()); // { x: 'x', a: 1, b: 2, c: 3 }

entries

Create an array of a given object's own enumerable string-keyed property key-value pairs.

Since: 0.1.0

Arguments

ParamTypeDescription
objectCollectionThe object to extract entries

Returns

TypeDescription
Array<[any, string | number]>Returns the array of key-value pairs

Declaration

declare const entries: <V = any, K extends string = string>(
object: Collection<V>
) => [V, K][];

Examples

const object = { a: 1, b: '2' };
const results = entries(object); // [['a', 1], ['b', '2']]

entriesIn

Create an array of a given object's own and inherited enumerable string-keyed property key-value pairs.

Since: 0.1.0

Arguments

ParamTypeDescription
objectCollectionThe object to extract entries

Returns

TypeDescription
Array<[any, string | number]>Returns the array of key-value pairs

Declaration

declare const entriesIn: <V = any, K extends string = string>(
object: Collection<V>
) => [V, K][];

Examples

function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
const results = entriesIn(new Foo()); // [['a', 1], ['b', '2'], ['c', 3]]

findKey

Returns the key of the first element predicate

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to inspect.
predicateFunctionThe function invoked per iteration.

Returns

TypeDescription
string | undefinedReturns the key of the matched element, else undefined.

Declaration

declare const findKey: <T extends object>(
object: T,
predicate: (value: any) => boolean
) => string | undefined;

Examples

const obj = {
john: { class: 1, grade: 'A' },
anna: { class: 3, grade: 'C' },
smith: { class: 2, grade: 'B' },
};
findKey(obj, (o) => 'class' in o); // 'john'
findKey(obj, (o) => o.class === 3); // 'anna'

findLastKey

Returns the key of the last element predicate

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to inspect.
predicateFunctionThe function invoked per iteration.

Returns

TypeDescription
string | undefinedReturns the key of the matched element, else undefined.

Declaration

declare const findLastKey: <T extends object>(
object: T,
predicate: (value: any) => boolean
) => string | undefined;

Examples

const obj = {
john: { class: 1, grade: 'A' },
anna: { class: 3, grade: 'C' },
smith: { class: 2, grade: 'B' },
};
findLastKey(obj, (o) => 'class' in o); // 'smith'
findLastKey(obj, (o) => o.class === 3); // 'anna'

get

Get the value at paths of object

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to query.
pathsstringThe path of the property to get.

Returns

TypeDescription
any | undefinedReturns the resolved value.

Declaration

declare const get: <T = any>(object: object, paths: string) => T | undefined;

Examples

const obj = { a: { b: [{}, { c: 3 }] } };
get(obj, 'a.b[1].c'); // 3

const pkg = { exports: { '.': { import: 'file' } } };
get(pkg, 'exports["."].import'); // 'file'

has

Checks if path is a direct property of object.

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to query
pathsstringThe path to check

Returns

TypeDescription
booleanReturns true if path exists, else false

Declaration

declare const has: (object: object, paths: string) => boolean;

Examples

const obj = { a: { b: c: 3 } }
has('a') // true
has('a.b.c') // true
has('a.b.d') // false

const pkg = { exports: { '.': { import: 'file' } } };
has(pkg, 'exports["."].import') // true

keys

Create an array of a given object's own enumerable string-keyed property names.

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to extract keys

Returns

TypeDescription
string[]Retruns the array of property names

Declaration

declare const keys: <T extends string>(object: object) => T[];

Examples

keys({ a: 1, b: 2 }); // ['a', 'b']

function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
keys(new Foo()); // ['a', 'b']

keysIn

Create an array of a given object's own and inherited enumerable property names.

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to extract keys

Returns

TypeDescription
string[]Retruns the array of property names

Declaration

declare const keysIn: <T extends string>(object: object) => T[];

Examples

keysIn({ a: 1, b: 2 }); // ['a', 'b']

function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
keysIn(new Foo()); // ['a', 'b', 'c']

omit

Create object omitted by given keys

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectobject
keys...stringThe property keys to omit.

Returns

TypeDescription
objectReturns omitted object

Declaration

declare const omit: <T extends object, K extends keyof T>(
object: T,
...keys: K[]
) => Omit<T, K>;

Examples

const obj = { a: 1, b: 2, c: 3 };
omit(obj, 'a', 'c'); // { b: 2 }

pick

Create object pickted by given keys

Since: 0.1.0

Arguments

ParamTypeDescription
objecobjectobject
keys...stringThe property keys to pick.

Returns

TypeDescription
objectReturns pickted object

Declaration

declare const pick: <T extends object, K extends keyof T>(
object: T,
...keys: K[]
) => Pick<T, K>;

Examples

const obj = { a: 1, b: 2, c: 3 };
pick(obj, 'a', 'c'); // { a: 1, c: 3 }

set

Sets the value at path of object.

Since: 0.1.0

Arguments

ParamTypeDescription
objectobjectThe object to modify.
pathsstringThe path of the property to set.
valueanyThe value to set

Declaration

declare const set: (object: object, paths: string, value: any) => void;

Examples

const obj = {};
set(obj, 'a.b[1].c', 3); // { a: { b: [undefined, { c: 3 }] } }

const arrMap = {};
set(arrMap, 'arr[-3]', 10); // { arr: [10, undefined, undefined] }

values

Create an array of a given object's own enumerable string-keyed property values.

Since: 0.1.0

Arguments

ParamTypeDescription
objectCollectionThe object to extract values

Returns

TypeDescription
any[]Returns the array of property values.

Declaration

declare const values: <T = any>(object: Collection<T>) => T[];

Examples

const object = { a: 1, b: '2' };
const results = values(object); // [1, '2']

valuesIn

Create an array of a given object's own and inherited enumerable string-keyed property values.

Since: 0.1.0

Arguments

ParamTypeDescription
objectCollectionThe object to extract values

Returns

TypeDescription
any[]Returns the array of property values.

Declaration

declare const valuesIn: <T = any>(object: Collection<T>) => T[];

Examples

function Foo() {
this.a = 1;
this.b = 2;
}
Foo.prototype.c = 3;
valuesIn(new Foo()); // [1, 2, 3]