# Can't (don't know how to) define sub-levels in objects

**URL:** <https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496>\
**Category:** General\
**Created:** [1 March 2019 09:54 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496 "2019-03-01T09:54:19Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [1 March 2019 09:54 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496/1 "2019-03-01T09:54:19Z")

</div>

Can someone give me/explain me the correct syntax to define till 2 sub-levels in the present code?

the array is : answer["gps-location"][0].constellation = []  
I would like to add under this level : nsat  
I tried without success with : answer["gps-location"][0].constellation.nsat = elements[0].substr(54,3)

```auto
let c;
answer["gps-location"] = [{}]
    if (answer.flag === "A") {
        let degreesN = Number(elements[0].substr(13,2))
        let minutesN = Number(elements[0].substr(15,7))
        let ns = elements[0].substr(22,1)                                            
        let degreesE = Number(elements[0].substr(23,3))
        let minutesE = Number(elements[0].substr(26,7))
        let ew = elements[0].substr(33,1)                                       
        answer["gps-location"][0].latitude = degreesN + minutesN/60                         
        if (ns === "S") answer.latitude = -answer.latitude
        answer["gps-location"][0].longitude = degreesE + minutesE/60
        if (ew === "W") answer.longitude = -answer.longitude        
}
        answer["gps-location"][0].speed = elements[0].substr(34,5)                                                                   
        answer["gps-location"][0].bearing = elements[0].substr(45,6)                                                                
        answer["gps-location"][0].constellation = []
            answer["gps-location"][0].constellation.nsat = elements[0].substr(54,3)

```

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 March 2019 09:57 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496/2 "2019-03-01T09:57:56Z")

</div>

```auto
answer["gps-location"][0].constellation = []

```

This is defining the property as a JavaScript Array.

You want it to be an Object - so you should define it as:

```auto
answer["gps-location"][0].constellation = {}

```

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [1 March 2019 10:04 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496/3 "2019-03-01T10:04:00Z")

</div>

> [@knolleary](#):
>
> answer["gps-location"][0].constellation = {}

Thanks knolleary!  
So, I also understood how to define as a sub-array!!!

Please, as the goal is to finally convert to XML, what is the best to correspond to the xml below in your opinion?

 ![gpsXML](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4c0607f8702b30efcb719e4b5dc3c50b32955e88.png)

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [1 March 2019 10:08 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496/4 "2019-03-01T10:08:34Z")

</div>

From [Convert to/from XML : Node-RED](https://cookbook.nodered.org/basic/convert-xml)

> When a particular XML output format is required, it can be easier to start by injecting that into the `XML` node to see the necessary JavaScript object required to generate it when fed back the other way.

---

<div class="post-metadata">

**Author:** ![jycemea](https://avatars.discourse-cdn.com/v4/letter/j/f475e1/32.png) [@jycemea](https://discourse.nodered.org/u/jycemea)\
**Post date:** [1 March 2019 10:11 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496/5 "2019-03-01T10:11:11Z")

</div>

Thanks! I will...

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [1 March 2019 10:39 UTC](https://discourse.nodered.org/t/cant-dont-know-how-to-define-sub-levels-in-objects/8496/6 "2019-03-01T10:39:44Z")

</div>

Given this question and the one you asked yesterday it would be worth working through one of the online tutorials about javascript objects and arrays.

[https://www.w3schools.com/js/js\_objects.asp](https://www.w3schools.com/js/js_objects.asp)
