Array Manipulation Functions
Lodash provides functions like _.map, _.filter, _.reduce, etc., for array manipulation. However, you can achieve similar functionality using methods like map, filter, and reduce directly available on JavaScript arrays.
The map Function
Transforms each collection element using a provided function, commonly used for array or object transformations.
_.map(collection, iterate)
The filter Function
Returns an array of elements satisfying a condition, useful for extracting specific elements from arrays or objects based on criteria.
_.filter(collection, predicate)
The reduce Function
Aggregates collection elements into a single value using an accumulation function, versatile for summing numbers, concatenating strings, or building complex objects.
_.reduce(collection, iteratee, [accumulator])
The head Function
Retrieves the first array element, handy for accessing initial array values without altering the original structure.
_.head(array)
The tail Function
Returns all array elements except the first one, useful for excluding initial elements from processing or analysis.
_.tail(array)
Example: The example below shows the Lodash feature which is Array Manipulation Functions that are available in plain JavaScript.
const _ = require('lodash');
// Plain JavaScript
const numbers = [1, 2, 3, 4, 5];
const doubled = numbers.map(num => num * 2);
const evenNumbers = numbers.filter(num => num % 2 === 0);
const sum = numbers.reduce((acc, curr) => acc + curr, 0);
const firstElement = numbers[0];
const tail = numbers.slice(1);
console.log("Plain JavaScript:");
console.log("Doubled:", doubled);
console.log("Even Numbers:", evenNumbers);
console.log("Sum:", sum);
console.log("First Element:", firstElement);
console.log("Tail:", tail);
// Lodash
const doubledLodash = _.map(numbers, num => num * 2);
const evenNumbersLodash = _.filter(numbers, num => num % 2 === 0);
const sumLodash = _.reduce(numbers, (acc, curr) => acc + curr, 0);
const firstElementLodash = _.head(numbers);
const tailLodash = _.tail(numbers);
console.log("\nLodash:");
console.log("Doubled:", doubledLodash);
console.log("Even Numbers:", evenNumbersLodash);
console.log("Sum:", sumLodash);
console.log("First Element:", firstElementLodash);
console.log("Tail:", tailLodash);
Output:
Plain JavaScript:
Doubled: [2, 4, 6, 8, 10]
Even Numbers: [2, 4]
Sum: 15
First Element: 1
Tail: [2, 3, 4, 5]
Lodash:
Doubled: [2, 4, 6, 8, 10]
Even Numbers: [2, 4]
Sum: 15
First Element: 1
Tail: [2, 3, 4, 5]
Lodash Features that are Available in JavaScript
Lodash is a JavaScript utility library that provides a wide range of functions. It offers an efficient API for working with arrays, objects, strings, functions, etc.
We’ll explore a few key features that are available in Lodash and demonstrate how they can be implemented using native JavaScript, reducing dependencies and improving code efficiency.
Table of Content
- Array Manipulation Functions
- Object Manipulation Functions
- Deep Cloning
- Function Debouncing and Throttling
Contact Us