How to create an array with multiple objects having multiple nested key-value pairs in JavaScript ?
Last Updated :
06 Dec, 2022
In this article, we will learn to create an array with multiple objects having key-value pairs, we will use nesting which means defining another array in a pre-defined array with key-value pairs.
Suppose we have several arrays of objects means containing various key-value pairs where keys are uniquely identified now we have to add these arrays to another array called the base or parent array. Let's do this with an example.
Example 1: Creating the array containing key-value pair objects.
JavaScript
<script>
const array1 = [ { key : 1 , value : "hello" } ,
{ key : 2 , value : "Geek" } ,
{ key : 3 , value : "GeeksforGeeks" } ,
{ key : 4 , value :"JavaScript"} ,
{ key : 5 , value : "Course" } ];
console.log(array1);
</script>
Output:
[ { key: 1, value: 'hello' },
{ key: 2, value: 'Geek' },
{ key: 3, value: 'GeeksforGeeks' },
{ key: 4, value: 'JavaScript' },
{ key: 5, value: 'Course' } ]
Example 2: Creating the arrays of objects and adding them to another array.
JavaScript
<script>
const array1 = [ { key : 1 , value : "hello" } ,
{ key : 2 , value : "Geek" } ,
{ key : 3 , value : "GeeksforGeeks" } ,
{ key : 4 , value :"JavaScript"} ,
{ key : 5 , value : "Course" } ];
const array2 = [ { key : 11 , value : "Courses" } ,
{ key : 12 , value : "C++" } ,
{ key : 13 , value : "JAVA" } ,
{ key : 14 , value : "C" },
{ key : 15 , value : "Many more" } ];
const array = [ array1 , array2 ];
console.log(array);
</script>
Output:
[ [ { key: 1, value: 'hello' },
{ key: 2, value: 'Geek' },
{ key: 3, value: 'GeeksforGeeks' },
{ key: 4, value: 'JavaScript' },
{ key: 5, value: 'Course' } ],
[ { key: 11, value: 'Courses' },
{ key: 12, value: 'C++' },
{ key: 13, value: 'JAVA' },
{ key: 14, value: 'C' },
{ key: 15, value: 'Many more' } ] ]
Here we have created two arrays containing various objects and then added these sub-arrays of objects to a base array or parent array.
Example 3: Printing the values of the nested array containing objects.
JavaScript
<script>
const array1 = [ { key : 1 , value : "hello" } ,
{ key : 2 , value : "Geek" } ,
{ key : 3 , value : "GeeksforGeeks" } ,
{ key : 4 , value :"JavaScript"} ,
{ key : 5 , value : "Course" } ];
const array2 = [ { key : 11 , value : "Courses" } ,
{ key : 12 , value : "C++" } ,
{ key : 13 , value : "JAVA" } ,
{ key : 14 , value : "C" },
{ key : 15 , value : "Many more" } ];
const array = [ array1 , array2 ];
for( let x = 0 ; x < array.length ; x++ )
{
for( let y = 0 ; y < array[0].length ; y++)
{
console.log( array[x][y].value );
}
}
</script>
Output:
hello
Geek
GeeksforGeeks
JavaScript
Course
Courses
C++
JAVA
C
Many more
Similar Reads
How to Create Array of Objects From Keys & Values of Another Object in JavaScript ? Creating an array of objects from the keys and values of another object involves transforming the key-value pairs of the original object into individual objects within an array. Each object in the array represents a key-value pair from the source object. Below approaches can be used to accomplish th
3 min read
How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app
5 min read
How to create an object from the given key-value pairs using JavaScript ? In this article, we will learn to create an object from the given key-value pairs using JavaScript.Key-value pair: Unique identifier (key) associated with its corresponding value; fundamental for data organization and retrieval in programming. here we have some common approaches.We will explore the
5 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ? Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsi
4 min read
How to Merge Child Object Value to Parent Key Based on Key Name in JavaScript ? JavaScript allows us to merge values from child objects into their parent object based on a specific key name. This can be useful for organizing data or simplifying complex structures. There are several approaches available in JavaScript to merge child object values to parent key based on the key na
4 min read
How to store a key=> value array in JavaScript ? In JavaScript, storing a key-value array means creating an object where each key is associated with a specific value. This allows you to store and retrieve data using named keys instead of numeric indices, enabling more intuitive access to the stored information.Here are some common approaches:Table
4 min read
How to Append an Object as a Key Value in an Existing Object in JavaScript ? In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows.
3 min read
How to Separate Array of Objects into Multiple Objects in JavaScript ? In JavaScript, the task of separating an array of objects into multiple objects involves organizing and restructuring data based on specific criteria or properties. This process is often necessary when dealing with datasets that need to be grouped or segmented for easier analysis or manipulation. Th
5 min read
How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met
5 min read
How to Create Dynamic Values and Objects in JavaScript? In JavaScript, you can choose dynamic values or variable names and object names and choose to edit the variable name in the future without accessing the array, Dynamic values and objects in JavaScript allow changing, accessing, or creating values and object properties at runtime for flexibility and
3 min read