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

131
Views
Flatten objects in array

Hey folks I am getting an array of objects from a response. I need to flatten all of the students objects to simply studentName but not certain how. Any help would be greatly appreciated.

Example Array:

[
 {
  students: {id: '123456', name: 'Student Name'},
  active: true
 },
 {
  students: {id: '123456', name: 'Student Name'},
  active: true
 }
]

What I am trying to do:

[
 {
  studentName: 'Student Name',
  active: true
 },
 {
  studentName: 'Student Name',
  active: true
 }
]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

[
  { students: {id: '123456', name: 'Student Name'}, active: true }, 
  { students: {id: '123456', name: 'Student Name'}, active: true }
].map(e => ({studentName: e.students.name, active: e.active}))
about 4 years ago · Juan Pablo Isaza Report

0

You can loop through the array and set each item's students property to the name property of the students property:

const arr = [
 {students: {id: '123456', name: 'Student Name'},active: true},
 {students: {id: '123456', name: 'Student Name'},active: true}
]


arr.forEach(e => e.students = e.students.name)

console.log(arr)

about 4 years ago · Juan Pablo Isaza Report

0

map over the data and return a new object on each iteration.

const data=[{students:{id:"123456",name:"Student Name"},active:!0},{students:{id:"123456",name:"Student Name"},active:!0}];

const out = data.map(obj => {

  // Destructure the name and active properties
  // from the object
  const { students: { name }, active } = obj;
  
  // Return the new object
  return { studentName: name, active };
});

console.log(out);

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!