How to use toArray function In Lodash
In this approach, we are using Lodash’s toArray function, which converts the values of an object into an array, resulting in an array representation of the object’s properties, useful for array operations and transformations.
Syntax:
_.toArray(value)
Example: The below example uses the toArray function to convert the object to an array in lodash.
const _ = require('lodash');
let obj = { a: 1, b: 2, c: 3 };
let arr = _.toArray(obj);
console.log(arr);
Output:
[ 1, 2, 3 ]
How to Convert Object to Array in Lodash ?
Converting an Object to an Array consists of changing the data structure from key-value pairs to an array format.
Below are the different approaches to converting objects to arrays in Lodash:
Table of Content
- Using toArray function
- Using values function
Run the below command before running the below code on your system:
npm i lodash
Contact Us