objectUtils

Description:
  • Set of utils for working with any kind of objects in SFCC.
Source:
Set of utils for working with any kind of objects in SFCC.

Methods

(inner) deepClone(data) → {*}

Description:
  • Deeply clones the given data.
Source:
Example
const data = { id: 1, category: { name: 'Dresses' }};
const clone = deepClone(data);
data.category.name = 'Shoes';
clone.category.name;  // 'Dresses'
Parameters:
Name Type Description
data * The data to be cloned.
Returns:
Returns the deeply cloned data.
Type
*

(inner) get(obj, path) → {*}

Description:
  • Retrieves a property from an object based on the provided path. Never throws any exceptions, but outputs retrieving property errors into logs.
Source:
Example
get(customer1, 'addressBook.preferredAddress.city');
get(product1, 'pageMetaTags[0].ID');
Parameters:
Name Type Description
obj Object The object from which to retrieve the property.
path string The path to the desired property. Use dot notation for object properties and square brackets with indices for array elements (e.g., 'property[0].nested.property').
Returns:
The value of the specified property if found; otherwise, returns undefined.
Type
*

(inner) isEqual(obj1, obj2) → {boolean}

Description:
  • Recursively checks if two objects are equal.
Source:
Example
isEqual({ id: 1, name: 'productName'}, { id: 1, name: 'productName'});
true
isEqual({ id: 1, category: { name: 'Other'}}, { id: 1, category: { name: 'Other'}});
true
isEqual({ id: 1}, { id: 1, category: { name: 'Other'}});
false
Parameters:
Name Type Description
obj1 Object The first object to compare.
obj2 Object The second object to compare.
Returns:
Returns `true` if the objects are equal, `false` otherwise.
Type
boolean

(inner) parseJSON(jsonString) → {Object|null}

Description:
  • Parses stringified JSON value. Never throws any exceptions, but outputs JSON parsing errors into logs.
Source:
Example
parseJSON('{ "countryCode":"CZ"}' );
// { "countryCode":"CZ" }
Parameters:
Name Type Description
jsonString string JSON string.
Returns:
Parsed JSON value or `null`.
Type
Object | null

(inner) pick(primaryObject, …args) → {Object}

Description:
  • Creates a new object by picking specific properties from a source object based on either a list of property names or a filtering function.
Source:
Example
Picking specific properties by name
pick({ id: 1, name: 'productName', size: 500 }, 'id', 'name');
{ id: 1, name: 'productName'}

Picking properties based on a filtering function
pick({ id: 1, name: 'productName', size: 500 }, (key, value) => value >= 500);
{ size: 500 }
Parameters:
Name Type Attributes Description
primaryObject Object The source object from which properties will be picked.
args string | function <repeatable>
Either an array of property names to pick, or a filtering function to determine inclusion.
Returns:
A new object containing only the selected properties from the source object.
Type
Object