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 }
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.
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)?