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

88
Vistas
sorting data from array of objects and push data from its value in JavaScript

I'm a beginner in javascript.

I have an some array of objects that looks like this:

[
  {
    id: 1,
    checkInStatus: 'pending',
    overtimeStatus: 'pending',
    checkOutStatus: 'success'
  },
  {
    id: 2,
    checkInStatus: 'success',
    overtimeStatus: 'pending',
    checkOutStatus: 'success'
  },
  {
    id: 3,
    checkInStatus: 'pending',
    overtimeStatus: 'success',
    checkOutStatus: 'success'
  }
]

And want to transform it into something like this:

[
  {
    id: 1,
    checkInStatus: 'pending'
  },
  {
    id: 1,
    overtimeStatus: 'pending'
  },
  {
    id: 2,
    overtimeStatus: 'pending'
   },
  {
    id: 3,
    checkInStatus: 'pending'
  }
]

I want to transform from the first data to the second data, how can this be achieved?

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

0

You could try something like this:

const yourData = [
  {
    id: 1,
    checkInStatus: 'pending',
    overtimeStatus: 'pending',
    checkOutStatus: 'success'
  },
  {
    id: 2,
    checkInStatus: 'success',
    overtimeStatus: 'pending',
    checkOutStatus: 'success'
  },
  {
    id: 3,
    checkInStatus: 'pending',
    overtimeStatus: 'success',
    checkOutStatus: 'success'
  }
]

const pending = [];
for(const entry of yourData){
  for(const property in entry){
    if(entry[property] === 'pending'){
      pending.push({ id: entry.id, [property]: entry[property] });
    }
  }
}
console.log(pending); // Matches your output requirement
about 4 years ago · Juan Pablo Isaza Denunciar

0

let data = [
  {
    id: 1,
    checkInStatus: 'pending',
    overtimeStatus: 'pending',
    checkOutStatus: 'success'
  },
  {
    id: 2,
    checkInStatus: 'success',
    overtimeStatus: 'pending',
    checkOutStatus: 'success'
  },
  {
    id: 3,
    checkInStatus: 'pending',
    overtimeStatus: 'success',
    checkOutStatus: 'success'
  }
]

To find the array of objects whose overtimeStatus is pending, you typically loop over the array elements using a for loop and test if the value of the overtimeStatus satisfies the condition, like this:

let yourData = [];
for (let i = 0; i < data.length; i++) {
    if (data[i].checkInStatus  === 'pending') {
        yourData.push(data[i]);
    }
}
console.log(yourData);
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