arrayCompact()arrayFlatten()arrayGenerate()dedup()deepCompare()deepCopy()mapToObj()objectClone()objectCopy()objectEach()objectFilter()objectIsValidPath()objectMergeAllOver()objectMergeKeys()objectMergeOver()objectMergePathsOver()objectMergePathsUnder()objectMergeUnder()objectOverlay()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.
lengthfnfn(index). The function returns the desired contents of the array element at 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.
mapToObj() 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. Unlike objectCopy(), the getters and setters are preserved as getters and setters.
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 deep is truthy, the returned array 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.
objfn (optional)fn(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.An Iterator for all enumerable object properties.
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(to, from, ...keys)
Copy the properties specified by the keys arguments from the from object to the target object.
targetfrom object.fromkeysfrom object to the target object.An object. The target object.
objectMergeOver() objectMergeOver(to, ...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. 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 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.
If the target object is {}, then a new object with the merge of all the sources objects is created.
The target object.
objectMergeUnder() objectMergeUnder(to, ...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. 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.
targetsourcestarget object.An object. The target object.
objectOverlay() objectOverlay(to, from, copy)
Copy the enumerable, own properties of the objects in from to the target object. Returns the merged target object. If copy is truthy, a deep copy of the target object is made before merging and the copy is returned. The merged copy recursively merges sub-objects as well.
targetfrom object.fromtarget object or a copy of the target object.copytarget objecy is made before merging.An object. The target object or a copy of it.
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.
For example, using:
const base = {
a: 1,
b: {ba:{baa: 2, bab:3}},
}
objectPath(base, ['b', 'ba']) will return {baa: 2}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.
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.
The obj object.
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.
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.