Enter a search term above to see results...
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.
filterObject
Filter an object based on a callback function.
Parameters
Name | Type | Description |
---|---|---|
obj | object | The object to filter |
callback | function | Function to test each key-value pair |
Returns
A new object with the key-value pairs that passed the test.
Example
weightedObjectSearch
Performs a weighted search across an array of objects, with matches prioritized by where they occur in the text.
Search Priority - Results are sorted by match quality, with highest priority given to exact start of string matches (e.g., searching “cat” matching “category”), followed by word-start matches (e.g., “category” in “my category”), then substring matches anywhere, and finally partial word matches. When searching multiple words, matches are weighted by how many words were found in the text.
Parameters
Name | Type | Description |
---|---|---|
query | string | The search query |
objectArray | array | Array of objects to search |
options | object | Search configuration |
Options
Name | Type | Default | Description |
---|---|---|---|
returnMatches | boolean | false | Include match details in results |
matchAllWords | boolean | true | Require all words to match |
propertiesToMatch | array | [] | Properties to search within objects |
Understanding Matches - You can use the option
returnMatches
to return details of how each result matched alongside the result.
Returns
Array of matching objects sorted by relevance.
Example
proxyObject
Create a proxy object that combines properties from a source object and a reference object.
Source Function Requirement The sourceObj parameter must be a function that returns the source object, not the object itself. This ensures the proxy always accesses the current state of the source object.
Parameters
Name | Type | Description |
---|---|---|
sourceObj | function | Function that returns the source object |
referenceObj | object | The reference object |