It implements some of the most commons functions of utility library like Lodash
Report Bug
·
Request Feature
It implements some of the most commons functions of utility library like Lodash
- Javascript
To get a local copy up and running follow these simple steps.
- Clone the repo
git clone https://github.com/luisitoguanes/js_utility_library.git
import _ from utils.js
Each: Receives an array or an object and a callback function. It will loop on each element and the callback will receive the current object being looped, the index, and the entire list. Doesn't return anything, it only executes the callback.
const arr = [1, 2, 3];
_.each(arr, (element) => { console.log(element);}); // 1 2 3
Filter: Receives an array or an object and a callback function. It will loop on each element and the callback will receive the current object being looped, the index, and the entire list. The callback must return true or false Will return an array with the objects that returned true from the callback.
const arr = [1, 2, 3];
const arrayLowerThanTwo = _.filter(arr, (element) => { if(element<=2) { return true; } else { return falses;}});
// returns [1, 2]
Map: Receives an array or an object and a callback function. It will loop on each element and the callback will receive the current object being looped, the index, and the entire list. Will return an array with the same length that the original array (or object keys) with the value transformed from the callback.
const arr = [1, 2, 3];
const arrayElementPlusOne = _.map(arr, (element) => { return element + 1;});
// returns [2, 3, 4]
Reduce: Receives an array or an object, a callback function, and an initial value. It will loop on each element and the callback will receive the current object being looped, the index, and the entire list. Return ONE object that accumulates the result of each iteration of the callback
const arr = [1, 2, 3];
const accumulative = _.reduce(arr, (element) => { return element + 1;}, 0);
// returns 6
Distributed under the MIT License.
Luis Guanes - @luisitoguanes - [email protected]
Project Link: https://github.com/luisitoguanes/js_utility_library