Sort the keys of an object manually

0

I have the following object array in my JavaScript code.

var data = [
   {
     ACC_OBSERVACIONES: 'Ninguna',
     COLOR: 'Rojo',
     MARCA: 'BMW',
     MODELO: 'i8',
     NOMBRE: 'Hoose'},
   {
     ACC_OBSERVACIONES: 'Ninguna',
     COLOR: 'Verde',
     MARCA: 'Merdedes Benz',
     MODELO: '32432',
     NOMBRE: 'Pedro'
   }
];

I would like to be able to change the order of the keys in my object. Is it possible to do this?

var data = [
    {
      MARCA: 'BMW',
      MODELO: 'i8',
      COLOR: 'Rojo',
      ACC_OBSERVACIONES: 'Ninguna',
      NOMBRE: 'Hoose'},
    {
      MARCA: 'Merdedes Benz',
      MODELO: '32432',
      COLOR: 'Verde',
      ACC_OBSERVACIONES: 'Ninguna',
      NOMBRE: 'Pedro'
    }
 ];

I noticed that the keys are alphabetized automatically, can I change the order of the keys my way? My problem is that I need to change the order of the keys because I use a plugin that loads the data of the variable, but it loads them in the order that they come and that order does not help me.

    
asked by Hoose 11.10.2017 в 21:47
source

2 answers

0

What you can do is get the DOM generated with PHP of the complete table and then apply the plugin to generate the filterable table.

In your ajax function, get the HTML code generated by PHP and then, select it using:

$(document).ready(function(){
$('#datatable table').datatable({
    pageSize: 5,
    sort: [true, true, false],
    filters: [true, false, 'select'],
    filterText: 'Type to filter... ',
    pagingDivSelector: "#paging-first-datatable"
});
});
<!-- CSS files (include only one of the two files!) -->
<!-- If you are not using bootstrap: -->
<link rel="stylesheet" type="text/css" href="http://holt59.github.io/datatable/css/datatable.min.css" media="screen">
<!-- If you are using bootstrap: -->
<link rel="stylesheet" type="text/css" href="http://holt59.github.io/datatable/css/datatable-bootstrap.min.css" media="screen">
                    
<!-- JS files -->       
<!-- Add the following if you want to use the jQuery wrapper (you still need datatable.min.js): -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript" src="http://holt59.github.io/datatable/js/datatable.jquery.min.js"></script>
<script src="http://holt59.github.io/datatable/js/datatable.min.js"></script>

<table id="datatable">
  <thead>
     <tr>
        <th>Marca</th>
        <th>Modelo</th>
        <th>Color</th>
        <th>ACC_Observaciones</th>
        <th>Nombre</th>
     </tr>
  </thead>
  <tbody>
     <tr>
        <td>BMW</td>
        <td>i8</td>
        <td>Rojo</td>
        <td>Ninguna</td>
        <td>Hoose</td>
     </tr>
     <tr>
        <td>Mercedes Benz</td>
        <td>32432</td>
        <td>Verde</td>
        <td>Ninguna</td>
        <td>Pedro</td>
     </tr>
  </tbody>
</table>
    
answered by 11.10.2017 / 22:29
source
0

You can remove all the key from your first element, then go through it and save it in another array,

  

THIS WILL ONLY WORK IF THE FIRST ELEMENT BRINGS DATA AND IT IS THE ORDER   WHAT YOU WANT

var data = [
    {
      MARCA: 'A',
      NOMBRE: 'Hoose'},
    {
      MARCA: 'Z',
      NOMBRE: 'Pedro'
    },
    {
     
      NOMBRE: 'Mantequilla',
      MARCA: 'C',
    }
 ];
 
 var keys = [];
 for (k in data[0]) {
  if (data[0].hasOwnProperty(k)) {
     keys.push(k);
  }
}
let longitudArray = data.length;
let cantidadKeys = keys.length;
let finalData = [];
for(let i = 0; i< longitudArray; i++){
  let json = {} 
  for (let j = 0; j < cantidadKeys; j++) {
    k = keys[j];
    json[k] = data[i][k];
  }
  finalData.push(json);
}


console.log(finalData)
    
answered by 11.10.2017 в 23:32