Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

86
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!