Creating an Array object with jQuery

0

Greetings community how are you doing? this time I come with a concern about arrays, it turns out that I'm creating an array from an event in a specific cell of a Datatable, I need to click on an ID field that I have in the datatable, all the data in that row can be converted into an array.

I show you the code that is responsible for obtaining the values of the selected row:

 $("td.clic").click(function(){
      var valores="";
      var identy="";
      var $objeto=[];

      // Obtenemos todos los valores contenidos en los <td> de la fila
      // seleccionada

      $(this).parents("tr").find("td").each(function(){

          identy=$(this).attr("id");
          valores=$(this).text();
          $objeto.push({index:identy,value:valores});

     });
  });

Well ... there the code initially fulfills its purpose, that is, I get the value of both the ID of the TD of the table and the value of each of the cells.

The problem arises when I want to make the array object, I want to get an array with a key-value structure where the key is an object.

However, the way I'm doing it, I get an array like this:

 Object { index: "id", value: "2" }
 Object { index: "tipoequipo", value: "MAQUINA DE RAYOS X" }
 Object { index: "marca", value: "PRUEBA" }
 Object { index: "serial", value: "1234578QWERTY" }
 Object { index: "bien", value: "4475121" }

I would like it to be like this:

id:"2"
tipoequipo:"MAQUINA DE RAYOS X"
marca: "PRUEBA"
serial: "1234578QWERTY"
bien: "4475121" 

How can I achieve it? Thank you in advance.

    
asked by jose angarita 12.12.2017 в 20:46
source

1 answer

1

What you are proposing is not creating an array, arrays are collections of values that you can access through a numeric index that corresponds to your position in the collection.

The equivalent of what you call a key-value array in javascript would be an object, where the names of the properties would be the key and the value of the property the value.

$(function(){
  $("td.clic").click(function(){
      // Obtenemos todos los valores contenidos en los <td> de la fila
      // seleccionada
      var objeto = {};
      $(this).parents("tr").find("td").each(function(){
          var identy=$(this).attr("id");
          var valores=$(this).text();
          if (identy){
            objeto[identy] = valores;
          }
     });
     console.log(objeto);
  });
});
.clic{
  background-color: #333333;
  color: white;
  cursor: pointer;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <tr>
    <td id="id">2</td>
    <td id="tipoequipo">MAQUINA DE RAYOS X</td>
    <td id="marca">PRUEBA</td>
    <td id="serial">1234578QWERTY</td>
    <td id="bien">4475121</td>
    <td class="clic">Botón 1</td>
  </tr>
  <tr>
    <td id="id">3</td>
    <td id="tipoequipo">MAQUINA 3</td>
    <td id="marca">PRUEBA 3</td>
    <td id="serial">3333333333</td>
    <td id="bien">333121</td>
    <td class="clic">Botón 2</td>
  </tr>
  <tr>
    <td id="id">4</td>
    <td id="tipoequipo">MAQUINA 4</td>
    <td id="marca">PRUEBA 4</td>
    <td id="serial">44444444</td>
    <td id="bien">444121</td>
    <td class="clic">Botón 3</td>
  </tr>
  <tr>
    <td id="id">5</td>
    <td id="tipoequipo">MAQUINA 5</td>
    <td id="marca">PRUEBA 5</td>
    <td id="serial">555555555</td>
    <td id="bien">555121</td>
    <td class="clic">Botón 4</td>
  </tr>
</table>
    
answered by 13.12.2017 в 09:11