On This Page
Object Utilities
The Object utilities provide a set of functions for working with objects in JavaScript. These functions help in manipulating, transforming, and querying objects efficiently.
Functions
keys
Return keys from an object.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to get keys from |
Returns
An array of the object’s keys, or undefined if the input is not an object.
Example
values
Return values from an object.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to get values from |
Returns
An array of the object’s values, or undefined if the input is not an object.
Example
filterObject
Filter an object based on a callback function.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to filter |
callback | function | The callback function to test each key-value pair |
Returns
A new object with the key-value pairs that passed the test.
Example
mapObject
Transform an object’s values based on a callback function.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to transform |
callback | function | The callback function to transform each value |
Returns
A new object with transformed values.
Example
extend
Extend an object with properties from other objects, properly handling getter/setters.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The target object to extend |
sources | …object | One or more source objects |
Returns
The extended object.
Example
pick
Create a new object with only the specified keys from the original object.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The source object |
keys | …string | The keys to pick |
Returns
A new object with only the specified keys.
Example
arrayFromObject
Convert an object to an array of key-value pairs.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to convert |
Returns
An array of key-value pair objects.
Example
get
Access a nested object field from a string path, like ‘a.b.c’.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to access |
path | string | The path to the desired property |
Returns
The value at the specified path, or undefined if not found.
Example
proxyObject
Create a proxy object that combines properties from a source object and a reference object.
Parameters
Name | Type | Description |
---|---|---|
sourceObj | function | A function that returns the source object |
referenceObj | object | The reference object |
Returns
A proxy object combining properties from both objects.
Example
onlyKeys
Create a new object with only the specified keys from the original object.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The source object |
keysToKeep | array | The keys to keep in the new object |
Returns
A new object with only the specified keys.
Example
hasProperty
Check if an object has a non-inherited property.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to check |
prop | string | The property name to check for |
Returns
True if the object has the property, false otherwise.
Example
reverseKeys
Reverse the keys and values of an object.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to reverse |
Returns
A new object with reversed keys and values.
Example
These object utilities provide a robust set of tools for working with objects in JavaScript, enhancing productivity and code readability.