Store data in a jquery array

1

I'm getting data from a table with the following code

$("#table_repuestos td").each(function(index) {
  alert($(this).text());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_repuestos">
  <thead>
    <tr>
      <th>Descripción</th>
      <th>Codigo</th>
      <th>Precio</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>DATO1</td>
      <td>DATO2</td>
      <td>DATO3</td>
    </tr>
     <tr>
      <td>DATO11</td>
      <td>DATO22</td>
      <td>DATO33</td>
    </tr>
  </tbody>
</table>

How can I store all the data obtained in an array?

EXAMPLE

var ParamObjSend = {                                                       
    'descripcion': 'DATO1',
    'codigo': 'DATO2',
    'Precio': 'DATO3',
};
    
asked by Javier Antonio Aguayo Aguilar 10.05.2017 в 18:03
source

2 answers

1

A Solution would be (explanation within the code)

 var array = [];
    /* Obtenemos todos los tr del Body*/
    var rowsBody= $("#table_repuestos").find('tbody > tr');
    /* Obtenemos todos los th del Thead */
    var rowsHead= $("#table_repuestos").find('thead > tr > th');
    
    /* Iteramos sobre as filas del tbody*/
    for (var i = 0; i < rowsBody.length; i++) {
        var obj={};/* auxiliar*/
        for (var j = 0;j < rowsHead.length;j++) /*  Iteramos sobre los th de THead*/
            /*Asignamos como clave el text del th del thead*/
             /*Asignamos como Valor el text del tr del tbody*/
            obj[rowsHead[j].innerText] =  rowsBody[i].getElementsByTagName('td')[j].innerText;
        array.push(obj);/* Añadimos al Array Principal*/
    }
    console.log(array);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_repuestos">
  <thead>
    <tr>
      <th>Descripción</th>
      <th>Codigo</th>
      <th>Precio</th>
      <th>Eliminar</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>DATO1</td>
      <td>DATO2</td>
      <td>DATO3</td>
      <td>DATO4</td>
      <td>DATO5</td>
    </tr>
      <tr>
      <td>DATO11</td>
      <td>DATO22</td>
      <td>DATO33</td>
      <td>DATO44</td>
    </tr>
  </tbody>
</table>
    
answered by 10.05.2017 / 19:55
source
1

It's pretty simple, just do the following:

// Creamos un objeto donde guardar primeramente las llaves
var obj = {};

// Creamos un arreglo donde guardar nuestros valores
var values = [];

// Recorremos todos los elementos th
$("#table_repuestos thead th").each(function(i, o) {

  // Obtenemos su respectivo texto
  var key = $(this).text(); 
  
  // Le asignamos como llave nuestro texto
  // y le damos un valor vacío
  obj[key] = '';

});

// Reocrremos todos los elementos td
$("#table_repuestos tbody td").each(function(i, o) {

  // Obtenemos su respectivo texto
  var value = $(this).text();
  // Agregamos nuestro valor al arreglo
  values.push(value);

});

var iterator = 0;

// Recorremos nuestro objeto
for(var key in obj){
  // Le asignamos el valor correspondiente
  obj[key] = values[iterator];
  iterator++;
}

console.log(obj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="table_repuestos">
  <thead>
    <tr>
      <th>Descripción</th>
      <th>Codigo</th>
      <th>Precio</th>
      <th>Eliminar</th>
    </tr>
  </thead>

  <tbody>
    <tr>
      <td>DATO1</td>
      <td>DATO2</td>
      <td>DATO3</td>
      <td>DATO4</td>
    </tr>
  </tbody>
</table>
  

Note The above example will work correctly as long as the number of TH and TD is exactly the same

    
answered by 10.05.2017 в 18:07