1

I have an array with registrations, inside this array is an array of students.
Now I want an array of all the students with only firstName, lastName and email.

registrations array

[
  0: {
    date: "2019-04-08T13:51:10.215Z"
    onlyVAT: false,
    students: [
      0: {
        email: "[email protected]",
        firstName: "Bennn",
        lastName: "test",
        phone: "0898989"
        ...
      }
    ]
  }
]

What I have so far:

 this.registrations.map(
    registration => registration.students.map(
        student => { return {
          firstName: student.firstName,
          lastName: student.lastName,
          email: student.email
          }}
      )
    );

but this returns an array of arrays

0: [
  0: {firstName: "Bennn", lastName: "test", email: "[email protected]"},
  1: ...
]

what I want is an array of (partial) student objects

[
  0: {firstName: "Bennn", lastName: "test", email: "[email protected]"},
  1: ...
]

ofcourse I could just loop and push to a new array but that's not what I want

5
  • Don't forget to assign the map result, it looks like it's not. Commented Jun 27, 2019 at 9:33
  • 1
    Should those be all student records from all registrations? Are there any duplicates? If yes, should they be removed? Commented Jun 27, 2019 at 9:38
  • @U25lYWt5IEJhc3RhcmQg It should be all students from all registrations, there are no duplicates, however, an examples that includes the removing of duplicates would be nice too! Commented Jun 27, 2019 at 9:40
  • also I don't know if this is the fastest solution, should I use filter instead? Commented Jun 27, 2019 at 9:54
  • @Ruben : if duplicates removal may be required, my code may do the job Commented Jun 27, 2019 at 10:48

4 Answers 4

2

Use flat() or flatMap(). Example:

  const newArr = registrations.map(
    registration => registration.students.map(
      student => { return {
        firstName: student.firstName,
        lastName: student.lastName,
        email: student.email
      }}
    )
  ).flat();

  console.log(newArr);
Sign up to request clarification or add additional context in comments.

Comments

1

Just use flatMap.

this.registrations.flatMap(...);

Or use reduce:

this.registrations.map(...).reduce((a, c) => [...a, c], []);

1 Comment

You can use [...a, c] instead of a.concat(c) 😎
1

Here is a solution with two Array.reduce() functions and Object.values(), which will ensure that the output will contain only unique emails (in the example input I have two identical emails [email protected]):

const registrations = [
  {
    date: '2019-04-08T13:51:10.215Z',
    onlyVAT: false,
    students: [
      {
        email: '[email protected]',
        firstName: 'Bennn',
        lastName: 'test',
        phone: '0898989'
      },
      {
        email: '[email protected]',
        firstName: 'Bennn2',
        lastName: 'test2',
        phone: '0898989'
      }
    ]
  },
  {
    date: '2019-05-08T13:51:10.215Z',
    onlyVAT: false,
    students: [
      {
        email: '[email protected]',
        firstName: 'Bennn3',
        lastName: 'test3',
        phone: '0898989'
      },
      {
        email: '[email protected]',
        firstName: 'Bennn4',
        lastName: 'test4',
        phone: '0898989'
      },
      {
        email: '[email protected]',
        firstName: 'Bennn4',
        lastName: 'test4',
        phone: '0898989'
      }
    ]
  }
];


const result = registrations.reduce((res, {students}) => ({
  ...res,
  ...students.reduce((res2, {email, firstName, lastName}) => ({
    ...res2,
    [email]: {email, firstName, lastName}
  }), {})
}), {});

console.log(Object.values(result));

Comments

0

The result is obtained in such form since students is a field inside an object in the array.

You could simply apply a combination of concat and ... spread operator on the result obtained from map to get the final array of students.

const studentsMapped = this.registrations.map(
    registration => registration.students.map(
        student => { return {
            firstName: student.firstName,
            lastName: student.lastName,
            email: student.email
       }}
    )
);

const students = [].concat(...studentsMapped);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.