I am working with an array of JavaScript objects that represent musical notes in my front-end development. To store this data, I had to convert the object array into a JSON string using JSON.stringify(objectArray)
before placing it into a hidden input field. However, this process automatically encloses all the keys in double quotes as shown below:
[
{"class":"barline","symbol":"standard","barline":true,"newSystem":true},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","endbar":true,"rebel":false},
{"class":"barline","symbol":"standard","barline":true},
{"class":"note","rhythm":"half","duration":0.5,"symbol":"flag","hand":"R","newbar":true,"rebel":false}
]
Before filtering the parameters in Rails, I use
JSON.parse(params[:score][:notes])
to convert the string back to a proper JSON array for storage in MongoDB (I'm using Mongoid).
Even though it is commonly advised to include keys in quotes, I prefer using dot notation to access values in JavaScript. Do you recommend switching to bracket notation or can you suggest a simple function that would remove the quotes from the keys before sending them to the hidden input?