Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda