I have hidden inputs which, through the name attribute, represent an array. I need to be able to iterate over each group.
My technique is to iterate through all found fields (noting their name identifier and value) and push into an array which I can then iterate over. I am able to select and find these inputs correctly, although ideally I would be able to fetch them all as a single array.
It may not necessarily be known how many groups there are.
The code below does not work because I am only left with the final construct (group) which is represented in multiple arrays.
<input type="hidden" data-type="item1" name="group[0][item1]" value="">
<input type="hidden" data-type="item2" name="group[0][item2]" value="">
<input type="hidden" data-type="item3" name="group[0][item3]" value="">
<input type="hidden" data-type="item1" name="group[1][item1]" value="">
<input type="hidden" data-type="item2" name="group[1][item2]" value="">
<input type="hidden" data-type="item3" name="group[1][item3]" value=">
<input type="hidden" data-type="item1" name="group[2][item1]" value="">
<input type="hidden" data-type="item2" name="group[2][item2]" value="">
<input type="hidden" data-type="item3" name="group[2][item3]" value="">
$(function () {
var i = 0;
while ($(this).find('input[name^=\'group[' + i + '\']').length) {
var arr = [];
var condition = $(this).find('input[name^=\'group[' + i + '\']');
$.each(condition, function () {
var type = $(this).attr('data-type');
var val = $(this).val();
arr.push({
[i]: {
[type]: val
}
});
});
i++;
}
});