-3

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
 }
]
Galactic Ranger
  • 819
  • 2
  • 11
  • 28
  • [{ 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})) – LeeKlaus Nov 19 '21 at 03:22
  • Familiarize yourself with [how to access and process nested objects, arrays or JSON](/q/11922383/4642212) and how to [create objects](//developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the available static and instance methods of [`Object`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](//developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Nov 19 '21 at 03:29

6 Answers6

1
[
  { 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}))
LeeKlaus
  • 136
  • 8
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)
Spectric
  • 27,594
  • 6
  • 14
  • 39
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);
Andy
  • 53,323
  • 11
  • 64
  • 89
0

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);
decpk
  • 21,436
  • 4
  • 19
  • 37
0

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));
tdc
  • 4,866
  • 10
  • 48
  • 96
0

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 })));
Mohammad Ali Rony
  • 4,066
  • 2
  • 17
  • 29