How to Convert Object Containing Objects into Array of Objects using Lodash?
Last Updated :
13 Sep, 2024
Lodash is a JavaScript utility library that provides predefined functions to make code more readable and cleaner. These functions are optimized for performance, often being faster than native JavaScript methods for complex operations.We will learn how to convert an object containing objects into an array of objects using Lodash.
There are two approaches to convert an object containing objects into an array of objects using Lodash:
Note: Before using them in your code, you should ensure that you have installed Lodash module. If not, you can install by using the following command:
npm install lodash
OR
yarn add lodash
By Using the _.map() function
You can convert an object containing objects into an array of objects using _.map function of Lodash. This function creates a new array with the results of calling a function on every element, allowing us to transform the data effectively.
Example: This example shows the use of _.map() function to convert the objects of objects into the array.
JavaScript
const _ = require('lodash');
const obj = {
a: { id: 1, name: "Alice" },
b: { id: 2, name: "Bob" },
c: { id: 3, name: "Charlie" }
};
const result = _.map(obj, (value) => value);
console.log(result);
Output:
[
{ id: 1, name: "Alice” },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" }
]
By Using the _.toArray() Method
You can also convert an object containing objects into an array of objects using the _.toArray() Method in Lodash. This function converts a given value into an array, making it more effective option for this task.
Example: This example shows the use of _.mtoArray() function to convert the objects of objects into the array.
JavaScript
const _ = require('lodash');
const obj = {
a: { id: 1, name: "Alice" },
b: { id: 2, name: "Bob" },
c: { id: 3, name: "Charlie" }
};
const result = _.toArray(obj);
console.log(result);
Output:
[
{ id: 1, name: "Alice” },
{ id: 2, name: "Bob" },
{ id: 3, name: "Charlie" }
]
By Using the _.values() Method
Another approach to convert an object containing objects into an array of objects is by using the _.values method in Lodash. This function creates an array of the own enumerable property values of an object.
Example:
JavaScript
const _ = require('lodash');
const obj = {
a: { id: 1, name: "Nikunj" },
b: { id: 2, name: "Dhruv" },
c: { id: 3, name: "Yash" }
};
const result = _.values(obj);
console.log(result);
Output:
[
{ id: 1, name: "Nikunj" },
{ id: 2, name: "Dhruv" },
{ id: 3, name: "Yash" }
]
By Using the _.entries() Method
The _.entries() method in Lodash can be used to convert an object into an array of key-value pairs, where each key-value pair is represented as an array. This can be further transformed into an array of objects containing both the key and the corresponding value, effectively converting the object containing objects into an array of objects.
Syntax:
_.entries(object)
Example: In this example, the _.entries() method is used to convert an object containing objects into an array of key-value pairs. These pairs are then mapped to a new array of objects, where each object includes both the key and the corresponding value.
JavaScript
const _ = require('lodash');
const obj = {
a: { id: 1, name: "Nikunj" },
b: { id: 2, name: "Dhruv" },
c: { id: 3, name: "Yash" }
};
const result = _.entries(obj).map(([key, value]) => ({
key: key,
...value
}));
console.log(result);
Output:
[
{ id: 1, name: "Nikunj" },
{ id: 2, name: "Dhruv" },
{ id: 3, name: "Yash" }
]
Similar Reads
How to Convert Array of Objects into Unique Array of Objects in JavaScript ?
Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has v
8 min read
How to Convert an Object into Array of Objects in JavaScript?
Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java:
3 min read
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 functionRun the below command before running the below
2 min read
How to Convert Object Array to Hash Map using Lodash ?
Converting an Object Array to a Hash Map consists of manipulating the array elements to create a key-value mapping. Below are the different approaches to Converting an object array to a hash map using Lodash:Table of Content Using keyBy() functionUsing reduce functionRun the below command before run
2 min read
How to Convert an Array of Objects into Object in TypeScript ?
Converting an array of objects into a single object is a common task in JavaScript and TypeScript programming, especially when you want to restructure data for easier access. In this article, we will see how to convert an array of objects into objects in TypeScript.We are given an array of objects a
3 min read
How to Convert Array into Array of Objects using map() & reduce() in JavaScript?
An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to c
2 min read
How to Replace Objects in Array using Lodash?
Replacing objects in an array using Lodash typically involves identifying the specific object(s) you want to replace and then performing the replacement operation. Lodash provides a range of methods to facilitate such operations, although the actual replacement might involve combining Lodash functio
3 min read
How to Convert an Array of Keys into a Union Type of Objects ?
We can convert an array of keys into a union type of objects in typescript using mapped types or object types. The process involves dynamically creating types based on the provided array of keys, resulting in a union type that represents all possible combinations of objects with those keys.Table of
3 min read
How to Update an Array of Objects with Another Array of Objects using Lodash?
Updating an array of objects with another array of objects involves merging or updating objects in the first array with corresponding objects from the second array. Below are the approaches to updating an array of objects with another array of objects in the Lodash library:Table of ContentUsing loda
4 min read
How to Convert returned JSON Object Properties to camelCase in Lodash?
When working with JSON data in JavaScript, it's common to need consistent property naming conventions, such as camelCase. Lodash provides utilities that can help with this transformation.These are the various approaches to convert object properties to camelCase using Lodash:Table of ContentUsing _.m
3 min read