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
function keys(obj)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
import { keys } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3 };console.log(keys(obj)); // ['a', 'b', 'c']values
function values(obj)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
import { values } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3 };console.log(values(obj)); // [1, 2, 3]filterObject
function filterObject(obj, callback)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
import { filterObject } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3, d: 4 };const result = filterObject(obj, (value) => value % 2 === 0);console.log(result); // { b: 2, d: 4 }mapObject
function mapObject(obj, callback)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
import { mapObject } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3 };const result = mapObject(obj, (value) => value * 2);console.log(result); // { a: 2, b: 4, c: 6 }extend
function extend(obj, ...sources)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
import { extend } from '@semantic-ui/utils';
const obj1 = { a: 1, b: 2 };const obj2 = { c: 3 };const obj3 = { d: 4 };const result = extend(obj1, obj2, obj3);console.log(result); // { a: 1, b: 2, c: 3, d: 4 }deepExtend
function deepExtend(obj, ...sources, options)Deep extends an object with properties from other sources, recursively merging nested plain objects and cloning non-plain objects.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | object | The target object to extend |
| sources | …object | One or more source objects to merge from |
| options | object | Optional configuration object |
Options
| Name | Type | Default | Description |
|---|---|---|---|
| preserveNonCloneable | boolean | false | Preserve custom class instances instead of flattening them |
Returns
The modified target object with deep merged properties.
Example
import { deepExtend } from '@semantic-ui/utils';
const target = { user: { name: 'Alice', age: 30 }, settings: { theme: 'light' }};
const source = { user: { role: 'admin' }, settings: { notifications: true }};
deepExtend(target, source);console.log(target);// {// user: { name: 'Alice', age: 30, role: 'admin' },// settings: { theme: 'light', notifications: true }// }pick
function pick(obj, ...keys)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
import { pick } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3, d: 4 };const result = pick(obj, 'a', 'c');console.log(result); // { a: 1, c: 3 }arrayFromObject
function arrayFromObject(obj)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
import { arrayFromObject } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3 };const result = arrayFromObject(obj);console.log(result); // [{ key: 'a', value: 1 }, { key: 'b', value: 2 }, { key: 'c', value: 3 }]get
function get(obj, path = '')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
import { get } from '@semantic-ui/utils';
const obj = { a: { b: { c: 42 } } };console.log(get(obj, 'a.b.c')); // 42console.log(get(obj, 'a.b.d')); // undefinedproxyObject
function proxyObject(sourceObj = noop, referenceObj = {})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
import { proxyObject } from '@semantic-ui/utils';
const source = () => ({ a: 1, b: 2 });const reference = { c: 3 };const proxy = proxyObject(source, reference);console.log(proxy.a); // 1console.log(proxy.c); // 3onlyKeys
function onlyKeys(obj, keysToKeep)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
import { onlyKeys } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3, d: 4 };const result = onlyKeys(obj, ['a', 'c']);console.log(result); // { a: 1, c: 3 }hasProperty
function hasProperty(obj, prop)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
import { hasProperty } from '@semantic-ui/utils';
const obj = { a: 1, b: 2 };console.log(hasProperty(obj, 'a')); // trueconsole.log(hasProperty(obj, 'toString')); // falsereverseKeys
function reverseKeys(obj)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
import { reverseKeys } from '@semantic-ui/utils';
const obj = { a: '1', b: ['2', '3'] };const result = reverseKeys(obj);console.log(result); // { '1': 'a', '2': 'b', '3': 'b' }These object utilities provide a robust set of tools for working with objects in JavaScript, enhancing productivity and code readability.
filterObject
function filterObject(obj, callback)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
import { filterObject } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3, d: 4 };const result = filterObject(obj, value => value % 2 === 0);console.log(result); // { b: 2, d: 4 }weightedObjectSearch
function weightedObjectSearch(query = '', objectArray = [], { returnMatches = false, matchAllWords = true, propertiesToMatch = []} = {})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
returnMatchesto return details of how each result matched alongside the result.
Returns
Array of matching objects sorted by relevance.
Example
import { weightedObjectSearch } from '@semantic-ui/utils';
const items = [ { title: 'Hello World', desc: 'A greeting' }, { title: 'World News', desc: 'Current events' }];
const results = weightedObjectSearch('world', items, { propertiesToMatch: ['title', 'desc']});proxyObject
function proxyObject(sourceObj = noop, referenceObj = {})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 |
Example
import { proxyObject } from '@semantic-ui/utils';
// Correct usage - passing a functionconst source = () => ({ a: 1, b: 2 });const proxy = proxyObject(source, { c: 3 });
// Incorrect usage - passing object directlyconst wrong = proxyObject({ a: 1 }, { c: 3 }); // Won't work as expected