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

142
Vistas
How to access second layer inside of the API

I have an API that looks like this:

{
"id": 749,
"firstName": "Ishna",
"lastName": "Willis",
"email": "Mawfly@awgawggwagov",
"phone": "(162)932-3460",
"adress": {
"streetAddress": "7006 Sed Ave",
"city": "Colfax",
"state": "WI",
"zip": "71085"
},
"description": "{lorem|32"
},

and I sort this API ascending and descending.

function sortInfo(e) {
  const column = e.target.dataset.column;
  const order = e.target.dataset.order;
  let changeData = e.target;

  if(order === 'desc') {
    changeData.setAttribute('data-order', 'asc');
    searchResult.fetchResult().then(response => {
     response.sort((a,b) => a[column] > b[column] ? 1 : -1);
     buildTable(response)
    })
  } else {
    changeData.setAttribute('data-order', 'desc');
    searchResult.fetchResult().then(response => {
      response.sort((a,b) => a[column] < b[column] ? 1 : -1);
      buildTable(response)
    })
  }
}

It works fine if I sort by name or surname, however, I also need to sort specifically by state. I have a data attribute but even if I make it adress.state when it is given to the column it wont work. Is there a way to refactor this code to access address.state?

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

0

I would suggest to create separate function to sort by address, then it will be easier to work on he data without adding tons of ifs inside of current function.

Another way that I can think about is to flat your object for sorting purpose then you will be able to sort by all of object properties and thed bring back original structure.

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