Object Utilities API reference for object manipulation functions box Guide

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

NameTypeDescription
objobjectThe 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

NameTypeDescription
objobjectThe 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

NameTypeDescription
objobjectThe object to filter
callbackfunctionThe 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

NameTypeDescription
objobjectThe object to transform
callbackfunctionThe 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

NameTypeDescription
objobjectThe target object to extend
sources…objectOne 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 }

pick

function pick(obj, ...keys)

Create a new object with only the specified keys from the original object.

Parameters

NameTypeDescription
objobjectThe source object
keys…stringThe 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

NameTypeDescription
objobjectThe 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

NameTypeDescription
objobjectThe object to access
pathstringThe 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')); // 42
console.log(get(obj, 'a.b.d')); // undefined

proxyObject

function proxyObject(sourceObj = noop, referenceObj = {})

Create a proxy object that combines properties from a source object and a reference object.

Parameters

NameTypeDescription
sourceObjfunctionA function that returns the source object
referenceObjobjectThe 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); // 1
console.log(proxy.c); // 3

onlyKeys

function onlyKeys(obj, keysToKeep)

Create a new object with only the specified keys from the original object.

Parameters

NameTypeDescription
objobjectThe source object
keysToKeeparrayThe 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

NameTypeDescription
objobjectThe object to check
propstringThe 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')); // true
console.log(hasProperty(obj, 'toString')); // false

reverseKeys

function reverseKeys(obj)

Reverse the keys and values of an object.

Parameters

NameTypeDescription
objobjectThe 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

NameTypeDescription
objobjectThe object to filter
callbackfunctionFunction 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

NameTypeDescription
querystringThe search query
objectArrayarrayArray of objects to search
optionsobjectSearch configuration
Options
NameTypeDefaultDescription
returnMatchesbooleanfalseInclude match details in results
matchAllWordsbooleantrueRequire all words to match
propertiesToMatcharray[]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

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

NameTypeDescription
sourceObjfunctionFunction that returns the source object
referenceObjobjectThe reference object

Example

import { proxyObject } from '@semantic-ui/utils';
// Correct usage - passing a function
const source = () => ({ a: 1, b: 2 });
const proxy = proxyObject(source, { c: 3 });
// Incorrect usage - passing object directly
const wrong = proxyObject({ a: 1 }, { c: 3 }); // Won't work as expected