I'm having trouble trying to create a dynamic associative array inside a loop, the error returned in the console is Cannot read property 'push' of undefined
, the idea is of a sports quiz with 3 questions for each team, I'm using the "data-time"
attribute that is equal in the 3 inputs of this team, so when sending the result would be created an array with the name of the team being the key and its "arrays children" would be the answers.
Example of inputs:
<input type="radio" name="respota1" value="respota1" data-time="flamengo">
<input type="radio" name="respota2" value="respota2" data-time="flamengo">
<input type="radio" name="respota3" value="respota3" data-time="flamengo">
Jquery:
var answers = $("input[data-time]:checked");
var items = new Array();
$.each( answers, function( key, value ) {
var time = $(this).attr('data-time');
switch(time) {
case 'flamengo':
items['flamengo'].push($(this).val());
break;
}
});
console.log(items);
How to proceed?