I'm trying to make an JS object out of all nested form elements in my Rails app. Rails serialises nested form elements like (this is a small sample view of my hidden form elements):
<input name="order[order_rules_attributes][0][quantity]" value="1" type="hidden">
<input name="order[order_rules_attributes][1][quantity]" value="3" type="hidden">
I'm sure that this Hash-like syntax can be converted in JS to an object like:
{
order: {
order_rules_attributes: {
[
{ quantity: 1 },
{ quantity: 3 }
]
}
}
}
But as this is no JSON syntax, nor a split()
-able string I don't know how to move on in this matter.
My question is: how do I convert this html to an JS object like mentioned?
PS: a suggestion has been made to use .serializeArray()
but this jQuery function only serialises form elements as a whole, whereas I'm looking to serialise the "name" attribute too.