Erwin Erwin - 1 month ago 5
jQuery Question

jQuery how to take all multiple form data in one array?

I want to have all data of my forms in one array

$('#teamMedewerker :selected').each(function(i){
var form = $("form#urenformulier" + i).serializeArray();
$.each($(form), function(i, field){
//console.log(field.name + " : " + field.value);
array = field.value;
console.log(array);
});
});


I want this result but dont know how to get this.

Array (employee1(test,test,test), employee2(test1,test2,test3))


each form is an employee, there are multiple forms.

Answer

If i understand correctly you should use map instead of each

var arrayOfEmployees = $('#teamMedewerker :selected').map(function(i){
    return [$("form#urenformulier" + i).serializeArray()];
}).get();

Now if you want to iterate over this you can do

arrayOfEmployees.forEach(function(employee){
    employee.forEach(function(field){
         console.log(field.name, field.value);
    }
});
Comments