I have a js object with an array property created as follows:
response.options = [];
response.options.push({
name: 'color',
nodes: this.colourNodes
}, {
name: 'size',
nodes: this.sizeNodes
});
I'm passing this to a rails endpoint through an ajax call, and saving it to my DB. The problem is that I need the JSON to be an array but currently it looks like this:
"options": {
"0": {
"name": "color",
"nodes": ["node1","node2","node3"]
},
"1": {
"name": "size",
"nodes": ["node1","node2","node3"]
}
}
I realize js arrays are are actually just objects which is why the above happens, however I can't seem to figure out a clean way to manipulate either the js or rails code to get this instead:
"options": [
{
"name": "color",
"nodes": ["node1","node2","node3"]
},
{
"name": "size",
"nodes": ["node1","node2","node3"]
}
]
I think the easiest solution is probably just to construct an array out of the options
object in rails but I can't seem to remove the root 0
and 1
properties.
I'm sure I'm missing something here, any help is appreciated
Thanks to @Mike C for his comment, here's what I ended up doing:
response.options = JSON.stringify(response.options);
And then just JSON.parse(params[:options])
on the rails side.
This seems crazy simple now that I have the answer but I was totally getting tunnel vision trying to stringify the whole object rather than just that property.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.