
Enter a search term above to see results...
Enter a search term above to see results...
The Object utilities provide a set of functions for working with objects in JavaScript. These functions help in manipulating, transforming, and querying objects efficiently.
function keys(obj)
Return keys from an object.
Name | Type | Description |
---|---|---|
obj | object | The object to get keys from |
An array of the object’s keys, or undefined if the input is not an object.
import { keys } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3 };console.log(keys(obj)); // ['a', 'b', 'c']
function values(obj)
Return values from an object.
Name | Type | Description |
---|---|---|
obj | object | The object to get values from |
An array of the object’s values, or undefined if the input is not an object.
import { values } from '@semantic-ui/utils';
const obj = { a: 1, b: 2, c: 3 };console.log(values(obj)); // [1, 2, 3]
function filterObject(obj, callback)
Filter an object based on a callback function.
Name | Type | Description |
---|---|---|
obj | object | The object to filter |
callback | function | The callback function to test each key-value pair |
A new object with the key-value pairs that passed the test.
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 }
function mapObject(obj, callback)
Transform an object’s values based on a callback function.
Name | Type | Description |
---|---|---|
obj | object | The object to transform |
callback | function | The callback function to transform each value |
A new object with transformed values.
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 }
function extend(obj, ...sources)
Extend an object with properties from other objects, properly handling getter/setters.
Name | Type | Description |
---|---|---|
obj | object | The target object to extend |
sources | …object | One or more source objects |
The extended object.
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 }
function pick(obj, ...keys)
Create a new object with only the specified keys from the original object.
Name | Type | Description |
---|---|---|
obj | object | The source object |
keys | …string | The keys to pick |
A new object with only the specified keys.
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 }
function arrayFromObject(obj)
Convert an object to an array of key-value pairs.
Name | Type | Description |
---|---|---|
obj | object | The object to convert |
An array of key-value pair objects.
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 }]
function get(obj, path = '')
Access a nested object field from a string path, like ‘a.b.c’.
Name | Type | Description |
---|---|---|
obj | object | The object to access |
path | string | The path to the desired property |
The value at the specified path, or undefined if not found.
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')); // undefined
function proxyObject(sourceObj = noop, referenceObj = {})
Create a proxy object that combines properties from a source object and a reference object.
Name | Type | Description |
---|---|---|
sourceObj | function | A function that returns the source object |
referenceObj | object | The reference object |
A proxy object combining properties from both objects.
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); // 3
function onlyKeys(obj, keysToKeep)
Create a new object with only the specified keys from the original object.
Name | Type | Description |
---|---|---|
obj | object | The source object |
keysToKeep | array | The keys to keep in the new object |
A new object with only the specified keys.
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 }
function hasProperty(obj, prop)
Check if an object has a non-inherited property.
Name | Type | Description |
---|---|---|
obj | object | The object to check |
prop | string | The property name to check for |
True if the object has the property, false otherwise.
import { hasProperty } from '@semantic-ui/utils';
const obj = { a: 1, b: 2 };console.log(hasProperty(obj, 'a')); // trueconsole.log(hasProperty(obj, 'toString')); // false
function reverseKeys(obj)
Reverse the keys and values of an object.
Name | Type | Description |
---|---|---|
obj | object | The object to reverse |
A new object with reversed keys and values.
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.
function filterObject(obj, callback)
Filter an object based on a callback function.
Name | Type | Description |
---|---|---|
obj | object | The object to filter |
callback | function | Function to test each key-value pair |
A new object with the key-value pairs that passed the test.
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 }
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.
Name | Type | Description |
---|---|---|
query | string | The search query |
objectArray | array | Array of objects to search |
options | object | Search configuration |
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.
Array of matching objects sorted by relevance.
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']});
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.
Name | Type | Description |
---|---|---|
sourceObj | function | Function that returns the source object |
referenceObj | object | The reference object |
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