Object Utilities Reference

Table of contents


Introduction

The object utility functions operate on classes, objects, and arrays. All object utility functions are properties of the global/window object and may be used without importing.


Functions

arrayCompact()

Syntax

arrayCompact(a)

Returns a copy of array a with all falsey elements removed.

Parameters

Return value

An array.


arrayFlatten()

Syntax

arrayFlatten(a)

Returns a copy of array a with the contents of array elements added in place of that array. The array elements are recursively processed in depth-first order as required to remove all array elements. Unlike the array flat() method, arrayFlatten(a) will flatten all nested arrays regardless of depth.

Parameters

Return value

An array.


arrayGenerate()

Syntax

arrayGenerate(length, fn)

Returns a new array of length length with the contents specified by the function fn.

Parameters

Return value

An array.


dedup()

Syntax

dedup(a)

Returns a copy of array a with duplicate elements removed.

Parameters

Return value

An array.


deepCompare()

Syntax

deepCompare(a, b)

Deeply compare values a and b. Values of different types return false. If the values are not arrays or objects, they are directly compared. However, numbers will return true if both are NaN. Arrays and the enumerable own keys of objects are recursively explored and compared.

Parameters

Return value

A Boolean. True if the two are identical.


deepCopy()

Syntax

deepCopy(value)

Return a deep copy of a value, object, or array. For arrays, all sub-arrays are recursively copied. For objects, the enumerable own properties of all nested objects are recursively copied, as well as any nested arrays. Other types are returned.

Parameters

Return value

Any type. A deep copy of the value.


inherits()

Syntax

inherits(ctor, superCtor)

A clone of the Node.js util.inherits() function. Makes the constructor function ctor inherit the prototype methods of the constructor function superCtor. It sets ctor.prototype to a new object whose prototype is superCtor.prototype (with a non-enumerable constructor property of ctor) and sets ctor.super_ to superCtor. Call it before adding methods to ctor.prototype. For ES6 classes, use extends instead.

Parameters


mapToObj()

Syntax

mapToObj(map)

Return an object with the Map keys an value transferred to object keys and values.

Parameters

Return value

An object..


objectClone()

Syntax

objectClone(obj)

Return a copy of an object that has the same prototype as obj. Unlike objectCopy(), all own property names, including non-enumerable properties, are copied and the getters and setters are preserved as getters and setters. Symbol keys are not copied. The copy is shallow.

Parameters

Return value

An object.


objectCopy()

Syntax

objectCopy(obj)
objectCopy(obj, deep)

Return a copy of an object. Unlike objectClone(), the getters and setters are not preserved and non-enumerable and non-own properties are ignored. If obj is null or undefined (and deep is not set), an empty object is returned. If deep is truthy, the returned object is a deep copy that recursively copies all sub-objects.

Parameters

Return value

An object.


objectEach()

Syntax

objectEach(obj, fn)

Apply a function, fn, to each enumerable own property in obj. Non-enumerable properties are ignored.

Parameters


objectFilter()

Syntax

objectFilter(obj, filter)

Filter the enumerable, own properties of obj and return a new object with only those properties. Getters and setter properties are not preserved. The filter can be a function or an array of allowed keys.

Parameters

Return value

A new plain object containing only the properties that passed the filter.


objectIsValidPath()

Syntax

objectIsValidPath(base, path)

Returns true if the array of keys in path form a valid path to a nested key in base.

Parameters

Return value

A Boolean.


objectMergeAllOver()

Syntax

objectMergeAllOver(target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. Returns the merged target object. The keys in the objects in the sources objects override any keys in the target object and earlier objects in the argument list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as get/set methods. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Unlike objectMergeOver() getters and setters are transferred as getters and setters.

Parameters

Return value

An object. The target object.


objectMergeKeys()

Syntax

objectMergeKeys(target, from, ...keys)

Copy the properties specified by the keys arguments from the from object to the target object. A key is copied only if it is present in from (tested with the in operator, so inherited properties count); keys not present in from are ignored. If the property value in from is the DELETE_KEY symbol, the corresponding key in the target object is deleted.

Parameters

Return value

An object. The target object.


objectMergeOver()

Syntax

objectMergeOver(target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. Returns the merged target object. The keys in the objects in the sources objects override any keys in the target object and earlier objects in the argument list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Unlike objectMergeAllOver() getters and setters are transferred as values, not getters and setters.

Parameters

Return value

An object. The target object.


objectMergePathsOver()

Syntax

objectMergePathsOver(paths, target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. The nested objects listed in the paths argument will be recursively merged over as well. The paths argument is an array of strings of the form used by objectPath() (e.g., 'a.b') to specify the nested objects in both the target and sources that will be recursively merged over as well.

The keys in a sources object override any keys in the target object and any earlier sources objects in the argument list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. If the property value in an args object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects. Returns the merged target object.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Parameters

Return value

The target object.


objectMergePathsUnder()

Syntax

objectMergePathsUnder(paths, target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object if they are not already present in the target object. The nested objects listed in the paths argument will be recursively merged under as well. The paths argument is an array of strings of the form used by objectPath() (e.g., 'a.b') to specify the nested objects in both the target and sources that will be recursively merged under as well.

The keys in the target object or in earlier sources objects will override keys in later objects in the arguments list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. DELETE_KEY is not supported.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Parameters

Return value

The target object.


objectMergeUnder()

Syntax

objectMergeUnder(target, ...sources)

Copy the enumerable, own properties of the objects in sources to the target object. Returns the merged target object. The keys in the target object or in earlier sources objects will override keys in later objects in the arguments list. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. DELETE_KEY is not supported.

If the target object is {}, then a new object with the merge of all the sources objects is created.

Parameters

Return value

An object. The target object.


objectOverlay()

Syntax

objectOverlay(target, ...sources)

This is a version of objectMergeOver() that also merges over any nested objects. Recursively copy the enumerable, own properties of the objects and sub-objects in sources to the target object. Returns the merged target object. The keys in the sources objects override any keys in the target object and earlier objects in the argument list. When a value in a source and the corresponding value in the target are both plain objects, they are recursively merged; otherwise the value from the source replaces the value in the target. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. If the property value in a sources object is the DELETE_KEY symbol, the corresponding key in the target object will be deleted, though the value may again be set by subsequent sources objects.

objectOverlay() modifies target. To avoid changing an existing object, pass a new object (e.g., {}) as the target. Note that sub-objects copied into the target from a source are shared with the source, not copied.

Parameters

Return value

An object. The target object.


objectUnderlay()

Syntax

objectUnderlay(target, ...sources)

This is a version of objectMergeUnder() that also merges any nested objects. Recursively copy the enumerable, own properties of the objects and sub-objects in sources to the target object if they are not already present in the target object. Returns the merged target object. The keys in the target object override any keys in sources, and keys in earlier sources objects override keys in later objects. When a value in a source and the corresponding value in the target are both plain objects, they are recursively merged. Ignores inherited properties and Symbol keys. Get/set methods are transferred as values, not functions. DELETE_KEY is not supported.

Parameters

Return value

An object. The target object.


objectPath()

Syntax

objectPath(base, path)
objectPath(base, path, value)

Get or set the value of a property in an object or in a nested object. path is an array of the keys to reference in each level of nesting to the property of interest. If no value is provided, it returns the value of the property specified by path. If a value is provided, the property in the nested object is set to value and the value is returned. If the value is present but set to the DELETE_KEY symbol, then the property is deleted from the nested object and undefined is returned. When setting a value, any intermediate objects missing from path are created. If path does not lead through plain objects (i.e., a bad path), an assert() fails.

For example, using:

const base = {
	a: 1,
	b: {ba:{baa: 2, bab:3}},
}

Parameters

Return value

The value of the property that is referenced by base and path. If the property was set, the new value. If the property was deleted using DELETE_KEY, undefined.


objectRenameKey()

Syntax

objectRenameKey(obj, oldKey, newKey)
objectRenameKey(obj, oldKey, newKey, value)

Rename oldKey in obj to newKey. If value is provided, the renamed property will have that value; otherwise, it will have the same value as the old property. If oldKey is not present in obj, then obj is not modified.

Parameters


objectTraverse()

Syntax

objectTraverse(obj, fn)
objectTraverse(obj, fn, baseObj)
objectTraverse(obj, fn, baseObj, path)

Traverse all properties in an object and any nested objects within it, calling fn on each one. Getters, setters, inherited properties, and Symbols are ignored. The path is an array of the keys required to get to the current nested object being enumerated, starting from baseObj using objectPath(). If the call to fn() returns a truthy value, objects below the current object are traversed. In most cases, objectTraverse() should be called with only the obj and fn arguments. The baseObj and path arguments are used by internal recursion or to continue a traverse down a specific path (e.g., an Array).

Parameters

Return value

None (undefined).


objToMap()

Syntax

objToMap(obj)

Return a new Map instance with all the enumerable own keys and values in obj.

Parameters

Return value

A Map instance with keys corresponding to the enumerable own properties in obj.


toArray()

Syntax

toArray(value)

toArray() ensures that value is processed as an array. If value is an array, then it is returned. If value is undefined, it returns an empty array. If value is something else, then toArray() returns an array with value as its single element.

Parameters

Return value

An array.


transpose()

Syntax

transpose(a)

Transposes a two-dimensional array. The array must have at least one level of nested arrays. Returns a new transposed array. If a is falsy, it is returned as is.

Parameters

Return value

An array.


useClassMethods()

Syntax

useClassMethods(ctor, obj, fn)

When a class instance that has a superclass is constructed, the superclass constructor will access the subclass' methods if it uses a method that the subclass overrides. This can lead to errors if the subclass methods rely on properties initialized in the subclass constructor. useClassMethods() allows a superclass to access its own methods while constructing. For example:

class Employee {
  constructor (name, group) {
    useClassMethods(Employee, this, function () {
      this.name = name;
      this.setGroup(group);
    })
  }
  setGroup (group) {
    this.group = group;
  }
}

class Manager extends Employee {
  constructor (name, group, subordinates) {
    super(name, group);
    this.subordinates = subordinates.map(name => new Employee(name, group));
  }
  setGroup (group) {
    super.setGroup(group);
    this.subordinates.forEach(sub => sub.setGroup(group));
  }
}
const mgr = new Manager('Fred', 'R&D', ['Wilma', 'Barney']);

Calling useClassMethods() in the Employee constructor allows it to use the Employee's setGroup() method rather than the Manager's setGroup() method when constructing a new Manager object. Calling the Manager's setGroup() method would result in an error as the subordinates property has not been initialized yet.

An alternative to solving similar problems would be to have constructors set up only the minimal information and use method chaining. For example, if the Employee and Manger constructor only set the name then, one could use:

const mgr = new Manager('Fred')
	.setGroup('R&D')
	.setSubordinates(['Wilma', 'Barney']);

However, this can sometimes be inconvenient.

Parameters


Constants

DELETE_KEY

A symbol denoting that a key should be deleted.