0

I know little about JS, still learning big time. However I am writing a NodeJS app that offers JSON web services to a Angular Frontend.

One problem I have is sometimes my objects have flat name/value pairs and sometimes my objects values will show up in Chromes Console as "address:Array[1]". While I can always do address[0] to get a value, there are parts of my code where I have both that and address: "12345" not sure how to handle it.

I would think it best to have all data output the same way but I cant seem to factor my node code to dump out flat name/value. Nor do I know how to create a Array[1] for a value.

This is the code that creates Array[1] values instead of flat name/value

    json.nodes.node.forEach(function (rowData) {
        dataObject.push({
            name: rowData.name
            ,address: rowData.address
            ,enabled: rowData.enabled
        })
    })   

This is the code that creates the flat name/value pairs

for (i = 60; i < 85; i++) {
    dataObject.push({
        name: i + "f"
        ,address: i
        ,enabled: true
    })

Any help would be great! Thanks in advance!

Eric Weintraub
  • 967
  • 1
  • 10
  • 23
  • The simple answer is that `rowData.address` contains an array; how to fix that, we'd have to see how it's being created. – JJJ Jan 28 '16 at 20:48
  • I did just figure out on the other end wrapping stuff in [ ] like address: [i] did make it pump out address.array[1] so for now it works. I would like to know if there is any solid way to systematically check if its an array of 1 and if so strip it down to just its value – Eric Weintraub Jan 28 '16 at 20:58
  • It would be preferable to fix the actual problem instead of monkey-patching unwanted behavior, but http://stackoverflow.com/questions/767486/how-do-you-check-if-a-variable-is-an-array-in-javascript – JJJ Jan 28 '16 at 21:21
  • the source looks like this: { '$': { flag: '128' }, address: [ '12345' ], name: [ 'item1' ] } this comes from XML parsing a rest API call from a blackbox I can not change – Eric Weintraub Jan 28 '16 at 21:50

0 Answers0