Create dynamic associative array within loop

Asked

Viewed 373 times

1

I’m having a hard time trying to create a dynamic associative array within a loop, the error returned in the console is Cannot read property 'push' of undefined, the idea is a sports quiz with 3 questions for each team, I’m using the attribute "data-time" which is equal in the 3 inputs of this team, so when sending the result an array would be created with the team name being the key and its "child arrays" 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?

1 answer

2


You are trying to insert something that does not exist in the array:

items['flamengo'] não existe

Another thing is that in this case where more than one item can be marked, it is more appropriate to use checkbox that is proper for this. The radio should be used only when choosing one of the options.

What you can do is create a new key this way from the checkbox checked and giving away push. Note that you don’t even need to use the switch:

var answers = $("input[data-time]:checked");
var items = new Array();
var conta = -1;
var flag;

$.each( answers, function( key, value ) {

   var time = $(this).attr('data-time');
   var val  = value.value;

   if(flag != time){
      flag = time;
      var o = {};
      o[time] = [];
      conta++;
      items.push(o);
   }

   items[conta][time].push(val);

});

console.log(items);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="respota1" value="respota1" data-time="flamengo">
<input type="checkbox" checked name="respota2" value="respota2" data-time="flamengo">
<input type="checkbox" name="respota3" value="respota3" data-time="flamengo">

<input type="checkbox" checked name="respota1" value="respota1" data-time="vasco">
<input type="checkbox" name="respota2" value="respota2" data-time="vasco">
<input type="checkbox" checked name="respota3" value="respota3" data-time="vasco">

  • I understood, but in the case there are really 3 different numbers, the idea is to capture the 3 values and insert into the items arrays, the key being the name of the type

  • Oh yes. So it’s the same thing I did in the answer. It will take what is checked and insert into the array.

  • He created numerical arrays and inside them the value of the team, the ideal would be like this: [ { "flamengo": { "answer1", "answer2", "answer3" } ;]

  • When I add another team a new case, it returns: Cannot read Property 'push' of Undefined

  • equal to those that exist, only that the "data-time" would be another, for example, data-time=""

  • Updated response.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.