Home > database >  Flatten objects in array
Flatten objects in array

Time:11-19

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
 }
]

CodePudding user response:

You can create and return a new array of result using map as:

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

const result = arr.map(({ students, ...rest }) => ({
  ...rest,
  studentName: students.name,
}));

console.log(result);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

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);
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

[
  { 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}))

CodePudding user response:

You can use the .map() method to rebuild the objects in the array with whatever structure you need.

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


function flatten(array) {
  return arr.map(({ students, active }) => {
    return {
      studentName: students.name,
      active,
    };
  });
}

console.log(flatten(arr));
<iframe name="sif4" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

CodePudding user response:

try this

let student =[
 {
  students: {id: '123456', name: 'Student Name'},
  active: true
 },
 {
  students: {id: '123456', name: 'Student Name'},
  active: true
 }
 ];
 
 console.log(student.map(x=> ({ studentName: x.students.name,active: x.active })));
<iframe name="sif5" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related