Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

172
Vistas
Sort the elements before inserting them into the table

I'm having a problem sorting the elements in the array based on the variable number. Once sorted, they must be inserted into the table. I try to use sort on item but I get the error.

I get the following error:

item.sort is not a function

How can I solve?

let myArr = [{
    type : "Image", 
    id_a: '123456', 
    number: 2, 
    id: 2,
    },{
    type: 'Text', 
    id_a: '123456', 
    number: 4, 
    id: 4
    },{
    type: 'Testo', 
    id_a: '564312', 
    number: 1, 
    id: 1},{
    type: 'Title', 
    id_a: '123456', 
    number: 3, 
    id: 3
    }
    ]
    
var id_url=123456;
var results = {};
for (var i = 0, len = myArr.length; i < len; i++) {
    var id_art = myArr[i].id_a;
    if (id_art == id_url) {
        results[i] = myArr[i];
        }
        
}

console.log(results);

 
Object.entries(results).forEach(item => {
    item = item[1];
    console.log(item)
    item.sort(function (a, b) {
           if (a.number != b.number) {
               return (a.number - b.number);
  }
 });
    let child = document.createElement("tr");
                 child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>`;
                    document.querySelector('#my-table').appendChild(child);
 })
<table id="my-table" width="90%">
                    <tr>
                        <th>Number</th>
                        <th>Type</th>
                        
                    </tr>

                </table>

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Your sorting function is correct but sort is a built-in function for arrays taking a function as the parameter. You can use it like below:

let myArr = [{
    type : "Image", 
    id_a: '123456', 
    value: 'moto_guzzi_v100_mandello.jpg', 
    number: 2, 
    id: 2,
    },{
    type: 'Text', 
    id_a: '123456', 
    value: 'The star of the Piaggio group stand is without doubt ... Discover all his secrets with our video', 
    number: 4, 
    id: 4
    },{
    type: 'Testo', 
    id_a: '564312', 
    value: 'Let find out together with our video', 
    number: 1, 
    id: 1},{
    type: 'Title', 
    id_a: '123456', 
    value: 'Moto Guzzi V100 Mandello, the queen of EICMA 2021', 
    number: 3, 
    id: 3
    }
    ]
    
var id_url=123456;
var results = {};
for (var i = 0, len = myArr.length; i < len; i++) {
    var id_art = myArr[i].id_a;
    if (id_art == id_url) {
        results[i] = myArr[i];
        }
        
}

console.log(results);

 
console.log("Before Sort", myArr);
const sortedArr =  myArr.sort(function (a, b) {
           if (a.number != b.number) {
               return (a.number - b.number);
  }
 });
console.log("After Sort", sortedArr);

about 4 years ago · Juan Pablo Isaza Denunciar

0

Try the following.

It's easier to sort.

      const filterList = myArr.filter((now) => now.id_a == id_url);
      const sortList = filterList.sort((a, b) => a.number - b.number);
      const results = { ...sortList };

      for (const item of sortList) {
        let child = document.createElement("tr");
        child.innerHTML = `
                    <td>${item.number}</td>
                    <td>${item.type}</td>`;
        document.querySelector("#my-table").appendChild(child);
      }
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda