Dynamic multi-dimensional associative array

Hello,

I’m trying to create a multi-dimensional associative array that has some of its data created dynamically. The code I’ve currently got is as follows (using JQuery to retrieve form element values):



var data = {

 label: $('#label').val(),
 child_float: function() {  
   $( 'input[id^=child_float]:checked' ).each( function( index ) {
                
       index: $(this).val();
                    
   });  
 }

}


The function is working fine, as I tested it on its own debugged to execute alert prompts. The aim here is for the child_float member to have child members 0, 1, 2 etc. In other words for it to look something like…


child_float: {
  0: 'left',
  1: 'right'
}

… if its values were not being created dynamically. However as you can tell that isn’t what is being generated at the moment, as there’s no opening/closing brace before/after the child_float’s member variables. Any ideas what changes I need to make in order make the child_float behave as intended?

Much thanks!

Awesome - that worked a treat!

You would need to perform the function behaviour later on, so that it can populate the data fields.

Something like the following should do the job.


var data = {
    label: $('#label').val(),
    child_float: []  
};
$( 'input[id^=child_float]:checked' ).each( function( index ) {
    data.child_float[index] = $(this).val();
});

Edit:

It might also me possible to use jQuery’s makeArray to streamline the process, but I haven’t investigated that yet.

Anyone? :confused: