arrayCompact()arrayFlatten()arrayGenerate()dedup()deepCompare()deepCopy()inherits()mapToObj()objectClone()objectCopy()objectEach()objectFilter()objectIsValidPath()objectMergeAllOver()objectMergeKeys()objectMergeOver()objectMergePathsOver()objectMergePathsUnder()objectMergeUnder()objectOverlay()objectUnderlay()objectPath()objectRenameKey()objectTraverse()objToMap()toArray()transpose()useClassMethods()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.
arrayCompact() arrayCompact(a)
Returns a copy of array a with all falsey elements removed.
aAn array.
arrayFlatten() 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.
aAn array.
arrayGenerate() arrayGenerate(length, fn)
Returns a new array of length length with the contents specified by the function fn.
lengthfn (optional)fn(index). The function returns the desired contents of the array element at index. If fn is omitted, each element in the generated array will contain its index.An array.
dedup() dedup(a)
Returns a copy of array a with duplicate elements removed.
aAn array.
deepCompare() 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.
abA Boolean. True if the two are identical.
deepCopy() 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.
valueAny type. A deep copy of the value.
inherits() 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.
ctorsuperCtormapToObj() mapToObj(map)
Return an object with the Map keys an value transferred to object keys and values.
mapAn object..
objectClone() 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.
objAn object.
objectCopy() 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.
objdeep (optional)An object.
objectEach() objectEach(obj, fn)
Apply a function, fn, to each enumerable own property in obj. Non-enumerable properties are ignored.
objfnfn(key, value, obj). The arguments are:keyvalueobjobjectFilter() 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.
objfilterfilter is an array, it contains the keys that should be transferred to the new object and other keys will be excluded. If filter is a function, it has the signature fn(key, value, obj). The arguments are:keyvalueobjfn() returns a truthy value, the returned object will include that property. Otherwise, the property will be excluded.A new plain object containing only the properties that passed the filter.
objectIsValidPath() objectIsValidPath(base, path)
Returns true if the array of keys in path form a valid path to a nested key in base.
basepathA Boolean.
objectMergeAllOver() 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.
targetsourcestarget object.An object. The target object.
objectMergeKeys() 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.
targetfrom object.fromkeysfrom object to the target object.An object. The target object.
objectMergeOver() 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.
targetsourcestarget object.An object. The target object.
objectMergePathsOver() 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.
pathstargetsourcestarget object.The target object.
objectMergePathsUnder() 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.
pathstargetsourcestarget object if the target object does not already have them.The target object.
objectMergeUnder() 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.
targetsourcestarget object if the target object does not already have them.An object. The target object.
objectOverlay() 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.
targetsourcestarget object.An object. The target object.
objectUnderlay() 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.
targetsourcestarget object if it does not already have them.An object. The target object.
objectPath() 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}},
}
objectPath(base, ['b', 'ba']) will return {baa: 2, bab: 3}objectPath(base, ['b', 'ba', 'bab']) will return 3.objectPath(base, ['b', 'ba', 'bab'], 4) will set base.b.ba.bab to 4 and return 4.basepathvalue (optional)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() 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.
objoldKeynewKeyoldKey.value (optional)obj[oldKey] is used.objectTraverse() 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).
obj
An object. The object traversed.
fn
A function that has the signature fn(curObj, curKey, value, baseObj, path). The arguments are:
curObjkeycurObj that is being examined.valuekey within curObj.baseObjpathbaseObj to curObj. If the value is an object (i.e., a nested object within curObj) and fn() returns a truthy value, then the object will be traversed further. Otherwise, the object in value is skipped.
None (undefined).
objToMap() objToMap(obj)
Return a new Map instance with all the enumerable own keys and values in obj.
objA Map instance with keys corresponding to the enumerable own properties in obj.
toArray() 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.
valueAn array.
transpose() 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.
aAn array.
useClassMethods() 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.
ctorfn.objthis) being constructed.fnobj. fn must not be an arrow function because it must use the passed this, not the lexical this.DELETE_KEY A symbol denoting that a key should be deleted.