0

Just when I think I've got he hang of identifying an element in an object, I run into a scenario that I cannot seem to get the value I want.

This part works and the data returned is correct: I have a map and when I attempt to identify a building on the map, I receive the following json object (this has been shortened for readability but in real life, its properly formatted): The function MapClick(queryResults) is called when the map is clicked.

dojo.io.script.jsonp_dojoIoScript19._jsonpCallback({
"results": [
    {
        "layerId": 5,
        "layerName": "Building",
        "value": "Name of item clicked",
        "displayFieldName": "name",
        "attributes": {
            "ID": "123",
            "name": "Name of item clicked",
            "Variable1": "Some bit of information",
            "Variable2": "Some other bit of information",
            ...
            ...

All I'm trying to do is return either the results[0].value OR results[0].attributes.name which in this example should bring back "Name of item clicked". The layerId, layerName, value, and displayFieldName are the "common most accessed data" so they are returned but the same information is also found within the attributes.

I've tried console.log(results[1].attributes.name); and console.log(results) with no success.

HPWD
  • 2,232
  • 4
  • 31
  • 61
  • 1
    You probably need `results.results[0].value`. The top level object has the property `results`. – Felix Kling Feb 24 '14 at 17:23
  • possible duplicate of [Access / process (nested) objects, arrays or JSON](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Felix Kling Feb 24 '14 at 17:24
  • Are you saying the top level will always be `results` and just so happens in this json object being returned, `results` is also used and that's what you are suggesting the `results.results[0].value` might work? In other words if the example provide above had been response, the answer might be `results.response[0].value`? – HPWD Feb 24 '14 at 17:30
  • The first `result` is the name of the variable you are using, which you can change however you want to. The second `result` is the name of the property in the returned data. – Felix Kling Feb 24 '14 at 18:08

1 Answers1

0

Turns out the name of the function handling the MapClicked is queryResults was needed so the correct answer is: queryResults[0].value and when you see open brackets [, you can be sure you will need [ some number ] (e.g. queryResults[0].value or queryResults[99].someothervariable. Or at least I think this is a correct statement.

HPWD
  • 2,232
  • 4
  • 31
  • 61