I have dynamic divs on a page which would have anassigned class from a bunch of values. For example:
<div class='class1'><span class='spn1'>test</span></div>
<div class='class2'><span class='spn2'>test another</span></div>
<div class='class2'><span class='spn3'>test1</span></div>
<div class='class3'><span class='spn4'>test123</span></div>
<div class='class1'><span class='spn221'>test</span></div>
The class could have any random number appended to it.
Now in my javascript, I am trying to build a dynamic JSON object based on the class of the div and the structure I want is:
{
class1: {
spn1: 'test',
spn221: 'test'
},
class2: {
spn2: 'test another',
spn3: 'test123'
},
class3: {
spn4: 'test223'
}
}
I am able to achieve this in a flat array structure but I want it in the JSON format as I will execute an ajax call based in the classes of div in other function. The flat array I am getting is as(which I don't want)
[{class:class1,span:spn1,text:test},{class:class1,span:spn221,text:test},...]
Link to fiddle: https://jsfiddle.net/8v0uove3/