Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To form an array of objects within an array of objects, you can follow these steps:

  1. Declare an array to store objects.
let arr = [];
  1. Define an object containing properties and values.
let obj = {property1: "value1", property2: "value2", property3: "value3"};
  1. Push the object into the array.
arr.push(obj);
  1. Repeat steps 2-3 for as many objects as you want in the array.
let obj2 = {property1: "value4", property2: "value5", property3: "value6"};
arr.push(obj2);
  1. To form an array of objects within the array, simply define an array inside the object, and repeat steps 2-4 for as many objects as you want in the sub-array.
let obj3 = {property1: "value7", property2: "value8", property3: "value9", 
            subArray: [{subProperty1: "subValue1", subProperty2: "subValue2"}, 
                      {subProperty1: "subValue3", subProperty2: "subValue4"}]};
arr.push(obj3);
  1. Access a specific object or property using array index notation and dot notation.
console.log(arr[2].subArray[1].subProperty2); // "subValue4"