object-helper-js v1.0.2
Object-Helper-JS
Control object like array in JavaScript.
const ObjectHelperJS = require('object-helper-js');
// Test Object
const Obj = {
'a': [1, 2, 3],
'b': [2, 3, 4],
'c': [3, 4, 5]
}
// forEach
Obj.forEach((value, key) => {
console.log('Object.prototype.forEach: ', `[${key}] => `, value);
});Object.prototype.forEach: [a] => [ 1, 2, 3 ]
Object.prototype.forEach: [b] => [ 2, 3, 4 ]
Object.prototype.forEach: [c] => [ 3, 4, 5 ]✨ Requirements
- Node.js: This package is a Node.js module. Before using it, You should install Node.js version 12 or higher.
🎯 Installation
$ npm install object-helper-js🎲 Methods
Accessor methods
Object.prototype.concat
- Reference: Array.prototype.concat()
Object.concat([value1[, value2[, ...[, valueN]]]]);Parameters
(Object) valueN: Objects to concatenate into a new object. If allvalueNparameters are omitted,concatreturns a shallow copy of the existing object on which it is called.
Return value
A new Object instance.
Object.prototype.filter
- Reference: Array.prototype.filter()
Object.filter(callback(value[, key[, object]])[, thisarg]);Parameters
(Callback) callback: A function to test for each element, taking three arguments:(Variable) value: The current value being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thatfilterwas called on.
[(Object) thisarg]: A object to use asthiswhen executingcallback.
Return value
A new object with the elements that pass the test. If no elements pass the test, an empty object will be returned.
Object.prototype.includes
- Reference: Array.prototype.includes()
Object.includes(valueToFind);Parameters
(Variable) valueToFind: The value to search for.
Return value
A Boolean which is true if the value valueToFind is found within the object.
Object.prototype.length
- Reference: Array.prototype.length()
Object.length();Return value
A Number which is length of object.
Iteration methods
Object.prototype.every
- Reference: Array.prototype.every()
Object.every(callback(value[, key[, object]])[, thisarg]);Parameters
(Callback) callback: A function to test for each element, taking three arguments:(Variable) value: The current value being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thateverywas called on.
[(Object) thisarg]: A object to use asthiswhen executingcallback.
Return value
true if the callback function returns a truthy value for every elements in object. Otherwise, false.
Object.prototype.find
- Reference: Array.prototype.find()
Object.find(callback(value[, key[, object]])[, thisarg]);Parameters
(Callback) callback: A function to test for each element, taking three arguments:(Variable) value: The current value being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thatfindwas called on.
[(Object) thisarg]: A object to use asthiswhen executingcallback.
Return value
The elements of the first element in the object that satisfies the provided testing function. Otherwise, undefined is returned.
Object.prototype.forEach
- Reference: Array.prototype.forEach()
Object.forEach(callback(value[, key[, object]])[, thisarg]);Parameters
(Callback) callback: Function to execute on each element. It accepts between one and three arguments:(Variable) value: The current value being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thatforEachwas called on.
[(Object) thisarg]: A object to use asthiswhen executingcallback.
Return value
undefined.
Object.prototype.keys
- Reference: Array.prototype.keys()
Object.keys([thisarg]);Return value
A new Array iterator object.
Object.prototype.map
- Reference: Array.prototype.map()
Object.map(callback(value[, key[, object]])[, thisarg]);Parameters
(Callback) callback: Function that is called for every element ofobject. Each time callback executes, the returned object is added tonew_object. Thecallbackfunction accepts the following arguments:(Variable) value: The current value being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thatmapwas called on.
[(Object) thisarg]: A object to use asthiswhen executingcallback.
Return value
A new object with each element being the result of the callback function.
Object.prototype.reduce
- Reference: Array.prototype.reduce()
Object.reduce(callback(accumulator, currentValue[, index[, object]])[, initialValue]);Parameters
(Callback) callback: Function that is called for every element ofobject. Each time callback executes, the returned object is added tonew_object. Thecallbackfunction accepts the following arguments:(Number) accumulator: The accumulator accumulates callback's return values. It is the accumulated value previously returned in the last invocation of the callback—orinitialValue, if it was supplied.(Variable) currentValue: The current element being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thatreducewas called on.
[(Number) initialValue]: A value to use as the first argument to the first call of thecallback. If noinitialValueis supplied, the first element in the array will be used and skipped. Callingreduceon an empty object without aninitialValuewill throw a0.
Return value
The single value that results from the reduction.
Object.prototype.some
- Reference: Array.prototype.some()
Object.some(callback(value[, key[, object]])[, thisarg]);Parameters
(Callback) callback: A function to test for each element, taking three arguments:(Variable) value: The current value being processed in the object.[(String) key]: The current key being processed in the object.[(Object) object]: The object thatsomewas called on.
[(Object) thisarg]: A object to use asthiswhen executingcallback.
Return value
true if the callback function returns a truthy value for at least one element in the array. Otherwise, false.
Object.prototype.values
- Reference: Array.prototype.values()
Object.values([thisarg]);Return value
A new Array iterator object.
Coding pleasure
Object.prototype.readSafely
Object.readSafely(...args);Parameters
(String | Number) ...args: Name of indices.
Return value
It returns a object which was processed with ...args. If the data not exists, It returns undefined.
Object.prototype.readRecursively
Object.readRecursively(...args);Parameters
(String | Number) ...args: Name of indices.
Return value
It returns a object which was processed with ...args. If the data not exists, It returns a empty object.
Object.prototype.writeRecursively
Object.writeRecursively(...args, value);Parameters
(String | Number) ...args: Name of indices.(Any) value: A value to write.
Return value
It returns a object which was processed with ...args and value. But, It is not required to rewrite it self like obj = obj.writeRecursively('test', 'app', 10);. It saves a result of processing to origin object.
🚩 Author
Dong-Hoon Yoo Developer
- Email: yoodonghoon01@gmail.com
- Blog: blog.donghoonyoo.com
- Roles: Development, Writing document in English and Korean.