I have a checkbox, each checkbox has a value of something like this: "case-color-id"
e.g.
- case1-red-1
- case2-blue-2
- case1-blue-4
- case3-green-3
What i'm trying to do is to create a json with those values. The end result would be something like:
cases:[
{
case1:[
{
id : '1'
color: 'red'
},
{
id: '4'
color: 'blue'
}
],
case2:[
{
id : '2'
color: 'blue'
}
],
case3:[
{
id : '3'
color: 'green'
}
]
}
]
I need help on the logic in inserting the objects with jquery/javascript
$('#checkbox :checkbox:checked').each(function (index, element) {
var case = $('this').val().split('-')[0];
var color = $('this').val().split('-')[1];
var id = $('this').val().split('-')[2];
var caseHolderObject = new Object();
// create objects here and insert the values depending which case it belongs to
// Then get the jsonString
var jsonString = JSON.stringify(caseHolderObject));
}