Console.log prints me a number between 0 instead of an array that passed through parameters

0

Good afternoon to everyone!

I have a problem with an array that passed me by parameters to a javascript function. The problem is that when I do console.log of the array inside the function, it gives me 0, but if I do it from where I call the function, it returns the array. Can someone help me please?

Javascript is not my strong point at the moment. This is the code:

jQuery(document).ready(function($){
  $('#mi-select').on('change',function(){
    $.post({
      url : mi_url.ajaxurl,
      data : {
        action: 'mi_funcion',
        param1: param[0],
        param2: param[1]
      }
    })
    .done( function(data){
      //Este console.log('data', data); me devuelve la informacion asi que el ajax esta bien
      //Llamo a una función creada por mi y le paso la array resultante del ajax
      new_table(data);
    })
    .fail( function(error) { console.log('error', error); });
  });
});

function extract_organized_data( data ){

  var array1 = new Array();
  var array2 = new Array();
  var columns = data.message.length;

  //Extraemos los datos y los organizamos en distintas arrays para las iteracciones al crear la tabla.
  for(c=0;c<data.message.length; c++){
    array1.push( data.message[c].asociativo1 );
    array2.push( data.message[c].asociativo2 );
  }

  //Preparamos lo que necesitamos devolver
  var tableData = new Array(array1,array2,columns);

  return tableData;
}

function new_table(data){

  var tableData = extract_organized_data(data);

  jQuery('#este-div').html(function(tableData){
    //Este console.log me devuelve un numero 0
    console.log(tableData);
  }
}

For more information, data is an array that contains an array by index (message) and for each index contains an associative array.

data{
  message:
    0: asociativo1:valor ,asociativo2:valor
    1: asociativo1:valor ,asociativo2:valor
}
    
asked by PiP 30.05.2018 в 16:02
source

1 answer

1

Instead of passing a parameter to the .html() function directly, do not place anything on it.

function new_table(data){

  var tableData = extract_organized_data(data);

  // elimino el parámetro ya que el primer valor está asignado para devolver el index
  jQuery('#este-div').html(function(){
    //Este console.log me devuelve un numero 0
    console.log(tableData);
  }
}
    
answered by 30.05.2018 / 16:41
source