Possible Duplicate:
How to create object property from variable value in javascript?
OK – this is a tough on for me. I am looping through all of my classes=”data” using jQuery. I then want to assign properties to my feature using each specific “dataName” and “dataValue”. However, once complete, I only have a a propery of “dataName=(the last value looped)”. How can I assign my dataName variable to create a new property each time – instead of thinking I only want to create a property named “dataValue”
function pushAttributesintoFeature(feature) {
$(".data").each( function(){
var dataName = $(this).attr("name")
var dataValue = $(this).val()
feature.dataName = dataValue
})
}
Example:
<input class="data" name="radius" value="15">
<input class="data" name="height" value="5">
Once I execute the script, I am left with: feature.dataName = "5"
But I want: feature.radius="15" AND feature.height = "5"
You can use the square bracket syntax to access a property with a variable:
This will set a property whose identifier is the value of
dataName, rather than the literal string “dataName”.