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

#1

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)

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)
0 Likes

#2
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:

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

#3

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?

0 Likes

#4

From 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.

0 Likes

#5

Thanks! I will...

0 Likes

#6

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

0 Likes