Select to view content in your preferred language

Need help in understanding this function

779
3
11-04-2011 10:36 AM
MikeOnzay
Frequent Contributor
I have questions about the code below. It is part of this example:

I added comments/questions in for myself.

var testVals={}; // create object literal (shortcut to creating a new object). This is to hold name:value pairs because an array can only hold single values?


var features = results.features; //features is a property of query task and has the type of 'graphic'. It is an array.
        dojo.forEach (features, function(feature) { //optimized way of doing a for loop, specific to dojo, patterned after HTML5. Let's you apply a function to each element of an array. Feature is each item in the array.
          zone = feature.attributes.ZONING_TYPE; //feature in this context is a graphic and attributes is a property
if (!testVals[zone]) { //tests to see if the name:value pair exists
            testVals[zone] = true; //if it it doesn't exist then set this object to true?
            values.push({name:zone}); //push the name:pair of name:<value of zoning type> into the object literal
          }


So, if zone has a value of 'residential' then when it gets tested in the if statement, is it testing to see if that value is contained in the testval object? Is it saying, "if zone value does not exist then set that value to true? I'm not exactly sure what exactly is getting set to true and why? Then, it pushes that value into the testval object using name:zone as the pair.
0 Kudos
3 Replies
JeffPace
MVP Alum
I have questions about the code below. It is part of this example:

I added comments/questions in for myself.

var testVals={}; // create object literal (shortcut to creating a new object). This is to hold name:value pairs because an array can only hold single values?


var features = results.features; //features is a property of query task and has the type of 'graphic'. It is an array.
        dojo.forEach (features, function(feature) { //optimized way of doing a for loop, specific to dojo, patterned after HTML5. Let's you apply a function to each element of an array. Feature is each item in the array.
          zone = feature.attributes.ZONING_TYPE; //feature in this context is a graphic and attributes is a property
if (!testVals[zone]) { //tests to see if the name:value pair exists
            testVals[zone] = true; //if it it doesn't exist then set this object to true?
            values.push({name:zone}); //push the name:pair of name:<value of zoning type> into the object literal
          }


So, if zone has a value of 'residential' then when it gets tested in the if statement, is it testing to see if that value is contained in the testval object? Is it saying, "if zone value does not exist then set that value to true? I'm not exactly sure what exactly is getting set to true and why? Then, it pushes that value into the testval object using name:zone as the pair.



almost

All your comments are correct, except the last two lines
Since testVals is JSON, and not an array
testVals[zone] = true
actually creates a key "zone" with value true

values.push({name:zone});
(you left out the var values=[]; ) pushes the json pair into an array

so you now have 2 variables, one with json values unsorted, and an array with the json pairs added in order.
0 Kudos
MikeOnzay
Frequent Contributor
almost

All your comments are correct, except the last two lines
Since testVals is JSON, and not an array
testVals[zone] = true
actually creates a key "zone" with value true

values.push({name:zone});
(you left out the var values=[]; ) pushes the json pair into an array

so you now have 2 variables, one with json values unsorted, and an array with the json pairs added in order.


How would I have known that this was JSON other than more experience and someone pointing it out to me?

Ok, so, testVals ends up looking like this: {name:residential, name:industrial, etc} or {residential:true, industrial:true, etc}

then the array:

values = [{"name:residential"},{name:industrial}]

and how does it sort (add them in order)?
0 Kudos
JeffPace
MVP Alum
How would I have known that this was JSON other than more experience and someone pointing it out to me?

Ok, so, testVals ends up looking like this: {name:residential, name:industrial, etc} or {residential:true, industrial:true, etc}

then the array:

values = [{"name:residential"},{name:industrial}]

and how does it sort (add them in order)?


JSON is {} with key value pairs.  It is just an object with some formatting (JavaScript Object Notation).

testVals = {"residential":true, "industrial":true} is correct.  Note that json is unsorted, so to retrieve the value you would say testVals.residential (which would return the boolean true in this case)

values is an array , and the push method adds values to the end as it loops through.  (unshift would add to the beginning) so values[0] would return JSON of {"residential":true}

so values[0].residential = true and values[1].industrial = true
0 Kudos