# How do I declare an array of objects?

**URL:** https://discourse.processing.org/t/how-do-i-declare-an-array-of-objects/22591
**Category:** Beginners
**Created:** [July 13, 2020, 6:12pm UTC](https://discourse.processing.org/t/how-do-i-declare-an-array-of-objects/22591 "2020-07-13T18:12:25Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![fabeck01](https://avatars.discourse-cdn.com/v4/letter/f/f19dbf/32.png) [@fabeck01](https://discourse.processing.org/u/fabeck01)
#### Post date: [July 13, 2020, 6:12pm UTC](https://discourse.processing.org/t/how-do-i-declare-an-array-of-objects/22591/1 "2020-07-13T18:12:25Z")

</div>

Hello,

I’m struggling with the definition of an array with the following (nested) structure - refer to picture:

 ![Array](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/6/602b6ab92c6af70ef5eda3d4cf1877a7d4f0b683.jpeg)

For each item in phase[] there should be:

- a title (string)
- a subtitle (string)
- an array of milestone [] (each a string)
- an array of engagement [] (each consisting of the following:)  
• a name (string)  
• a purpose (string)  
• a deliverable (string)

As an example, I’d like to access the variable phase[1].engagement[1].deliverable to get the value of the deliverable of engagement[1] of phase[1].  
Or phase[0].subtitle should be the subtitle of phase[0].

Would anyone kindly be able to help?

Thanks!

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [July 13, 2020, 7:37pm UTC](https://discourse.processing.org/t/how-do-i-declare-an-array-of-objects/22591/2 "2020-07-13T19:37:20Z")

</div>

Each index in _phase[]_ is an object w/ many properties.

And 1 of them is another array named _engagement[]_ containing 2 objects w/ many properties too.

In order to facilitate the creation of those 2 sets of object properties we can define a different `class` for each of both:

> **[class](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/class)**
>
> The class declaration creates a new class with a given name using prototype-based inheritance.

Starting w/ the inner _engagements[]_ we can define an Engagement `class` w/ its **constructor()** accepting the following parameters: `(name, purpose, deliverable)`:

```auto
class Engagement {
  constructor(name, purpose, deliverable) {
    this.name = name;
    this.purpose = purpose;
    this.deliverable = deliverable;
  }
}

```

We do the same for the _phases[]_ array, defining a `class` named Phase:

```auto
class Phase {
  constructor(title, subtitle, milestones, engagements) {
    this.title = title;
    this.subtitle = subtitle;
    this.milestones = milestones;
    this.engagements = engagements;
  }
}

```

Now we declare the array variables we’re gonna need to store the instances of those 2 classes:

```auto
const PHASES = 2, phases = Array(PHASES).fill();
var engagements;

```

Before instantiating our 2 Phase objects we 1st need to instantiate an Engagement using the keyword `new` twice passing the required arguments for its **constructor()** and storing them in the _engagements[]_ array:

```auto
engagements = [
  new Engagement('Wilson', 'marry', true),
  new Engagement('Sarrah', 'marry', true)
];

```

After that we can instantiate our 1st Phase and store it in the 1st index of _phases[]_:

```auto
phases[0] = new Phase('Happy Forever!', 'Engaged', [], engagements);
console.table(engagements);

```

We repeat the steps again for the 2nd index of _phases[]_:

```auto
engagements = [
  new Engagement('John', 'travel', false),
  new Engagement('Mary', 'travel', false)
];

phases[1] = new Phase('Savannah Adventures', 'Wilderness', [], engagements);
console.table(engagements);

console.table(phases);

```

Now we can further inspect the contents of the _phases[]_ array like this:

```javascript
console.log(phases[1].engagements[1].deliverable);
console.log(phases[0].subtitle);

```

---

<div class="post-metadata">

### Author: ![fabeck01](https://avatars.discourse-cdn.com/v4/letter/f/f19dbf/32.png) [@fabeck01](https://discourse.processing.org/u/fabeck01)
#### Post date: [July 14, 2020, 1:54pm UTC](https://discourse.processing.org/t/how-do-i-declare-an-array-of-objects/22591/3 "2020-07-14T13:54:37Z")

</div>

Thank you very much, GoToLoop, for the comprehensive reply. I’ll have to digest it first and try around a little before I can even properly appreciate your kind reply.
