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 ]
- Node.js: This package is a Node.js module. Before using it, You should install Node.js version 12 or higher.
$ npm install object-helper-js
- Reference: Array.prototype.concat()
Object.concat([value1[, value2[, ...[, valueN]]]]);
-
(Object) valueN
: Objects to concatenate into a new object. If allvalueN
parameters are omitted,concat
returns a shallow copy of the existing object on which it is called.
A new Object
instance.
- Reference: Array.prototype.filter()
Object.filter(callback(value[, key[, object]])[, thisarg]);
-
(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 thatfilter
was called on.
-
-
[(Object) thisarg]
: A object to use asthis
when executingcallback
.
A new object with the elements that pass the test. If no elements pass the test, an empty object will be returned.
- Reference: Array.prototype.includes()
Object.includes(valueToFind);
-
(Variable) valueToFind
: The value to search for.
A Boolean
which is true
if the value valueToFind
is found within the object.
- Reference: Array.prototype.length()
Object.length();
A Number
which is length of object.
- Reference: Array.prototype.every()
Object.every(callback(value[, key[, object]])[, thisarg]);
-
(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 thatevery
was called on.
-
-
[(Object) thisarg]
: A object to use asthis
when executingcallback
.
true
if the callback function returns a truthy value for every elements in object. Otherwise, false
.
- Reference: Array.prototype.find()
Object.find(callback(value[, key[, object]])[, thisarg]);
-
(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 thatfind
was called on.
-
-
[(Object) thisarg]
: A object to use asthis
when executingcallback
.
The elements of the first element in the object that satisfies the provided testing function. Otherwise, undefined
is returned.
- Reference: Array.prototype.forEach()
Object.forEach(callback(value[, key[, object]])[, thisarg]);
-
(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 thatforEach
was called on.
-
-
[(Object) thisarg]
: A object to use asthis
when executingcallback
.
undefined
.
- Reference: Array.prototype.keys()
Object.keys([thisarg]);
A new Array
iterator object.
- Reference: Array.prototype.map()
Object.map(callback(value[, key[, object]])[, thisarg]);
-
(Callback) callback
: Function that is called for every element ofobject
. Each time callback executes, the returned object is added tonew_object
. Thecallback
function 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 thatmap
was called on.
-
-
[(Object) thisarg]
: A object to use asthis
when executingcallback
.
A new object with each element being the result of the callback function.
- Reference: Array.prototype.reduce()
Object.reduce(callback(accumulator, currentValue[, index[, object]])[, initialValue]);
-
(Callback) callback
: Function that is called for every element ofobject
. Each time callback executes, the returned object is added tonew_object
. Thecallback
function 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 thatreduce
was called on.
-
-
[(Number) initialValue]
: A value to use as the first argument to the first call of thecallback
. If noinitialValue
is supplied, the first element in the array will be used and skipped. Callingreduce
on an empty object without aninitialValue
will throw a0
.
The single value that results from the reduction.
- Reference: Array.prototype.some()
Object.some(callback(value[, key[, object]])[, thisarg]);
-
(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 thatsome
was called on.
-
-
[(Object) thisarg]
: A object to use asthis
when executingcallback
.
true
if the callback function returns a truthy value for at least one element in the array. Otherwise, false
.
- Reference: Array.prototype.values()
Object.values([thisarg]);
A new Array
iterator object.
Object.readSafely(...args);
-
(String | Number) ...args
: Name of indices.
It returns a object which was processed with ...args
. If the data not exists, It returns undefined
.
Object.readRecursively(...args);
-
(String | Number) ...args
: Name of indices.
It returns a object which was processed with ...args
. If the data not exists, It returns a empty object.
Object.writeRecursively(...args, value);
-
(String | Number) ...args
: Name of indices. -
(Any) value
: A value to write.
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.
- Email: yoodonghoon01@gmail.com
- Blog: blog.donghoonyoo.com
- Roles: Development, Writing document in English and Korean.