How to create an array of objects with all objects from multiple arrays?

Charles Braga

Given de following arrays:

const classes = [
  {
    students: [
       Student1,
       Student2,
    ]
  },
  {
    students: [
       Student3,
       Student4,
    ]
  }
]

What's the best way performance-wise to generate an array of objects with a similar structure to this:

const allStudents = [
  Student1,
  Student2,
  Student3,
  Student4
]

Nitheesh

Please note: Donot use reserved keyworks such ass class as a variable name.

Array.reduce implementation

const classes = [
  { students: [ 'Student1', 'Student2' ] },
  { students: [ 'Student3', 'Student4' ] }
];
const allStudents = classes.reduce((acc, cls) => {
  acc = cls.students ? acc.concat(cls.students) : acc;
  return acc;
}, []);
console.log(allStudents)

Array.flatMap implementation.

const classes = [
  { students: [ 'Student1', 'Student2' ] },
  { students: [ 'Student3', 'Student4' ] }
]
const allStudents = classes.flatMap((cls) => cls.students ? cls.students : []);
console.log(allStudents);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to get all values from an array of objects with nested arrays of objects?

How to create an array of arrays, from an array of objects using .map?

How do I create new arrays of objects with objects from an existing array, based on values in the objects?

How to create JSON or Array of Objects From Three JS Arrays

How to create array of objects from object of objects?

Create array of object from 3 arrays of objects

How do I create an flattened array of arrays that are part of an object in an array, essentially get the child arrays of multiple objects?

How to create an array of objects with 2 arrays?

How to get all specific values from an array of objects with nested arrays of objects?

JavaScript - How to put all of the objects from several nested arrays into a new array of unique objects

How to extract all possible matching arrays of objects from one array of objects?

Create new array of arrays from arrays of objects of array , with specific value

How to create multiple objects from array's value in javascript?

How to destructure all of the objects from the nested arrays?

How to create multiple array of objects from single array of objects based on property value

Create Object multiple objects from a array list

How to fetch the all objects from the array of array?

Creating array of arrays of objects from array of objects

Multiple arrays of objects inside array of objects in MongoDB

How to create array of objects from array: javascript

How to create array of JSON objects with multiple values?

How to create multiple array of objects in Mongoose?

How to create an array of arrays out of array of objects in JavaScript not chunked array

Create arrays from JavaScript objects

Create an tree of objects from arrays

How to remove all dupes from an array of objects?

How to get unique objects from array of objects by all properties?

How to create elements from an array of objects in React?

How to create a type from an array of objects in TypeScript?