0

I have two nested object arrays, one is an array describing a school (id, name, tests it conducts along with their counts), and another is an array of school teachers corresponding to the schools in the first array. Here is some sample data for both arrays:

import { ObjectId } from 'mongodb'

monthlySchoolTests = [{
  schoolId: ObjectId('804d8527f390ghz26e3426j6'),
  schoolName: "School1"
  schoolTests: [
    { testName: "Chemistry", count: 15 },
    { testName: "Music", count: 8 }
  ]
 },
 {
  schoolId: ObjectId('804ef074384b3d43f125ghs5'),
  schoolName: "School2"
  schoolTests: [
    { testName: "Math", count: 3 },
    { testName: "Physics", count: 12 },
    { testName: "Biology", count: 10 }
  ]
 }
]

schoolTeachers = [{
  schoolId: ObjectId('804ef074384b3d43f125ghs5')
  schoolName: "School2"
  teachers: [
    { name: "Michael", email: "michael@school2.edu" },
    { name: "Jane", count: "jane@school2.edu" },
    { name: "Lukas", count: "lukas@school2.edu" }
  ]
 },
 {
  schoolId: ObjectId('804d8527f390ghz26e3426j6')
  schoolName: "School1"
  teachers: [
    { name: "Cleo", email: "cleo@school1.edu" },
    { name: "Celine", count: "celine@school1.edu" },
    { name: "Ike", count: "ike@school1.edu" }
  ]
 }
]

I obtained the second array by passing as an argument an array of schoolIds that I got from the query results for the first array, so I know that both arrays have about the same number of objects (assuming queries don't return null values). I am trying to link the test information in the first array with the school teacher in the second array but the .find() method is not working for me. Here is my code:

monthlySchoolTests.map(testObj => {
  const selectedSchoolTeachers = schoolTeachers.find(teacherObj => {
    String(testObj.schoolId) === String(teacherObj.schoolId)
  })

  console.log(selectedSchoolTeachers)
})

For some reason, I only get undefined even though I have tested this by mapping through each object in the first array and asserting that there is a match for every schoolId in the second array. (console.log yields true).

Sorry for any errors as this is my first time posting. Would appreciate any help!

  • 1
    You're not returning anything from your `find` callback. – Andy Jun 24 '21 at 08:07
  • 1
    Also, please don't misuse `map` like that. If you're not using the array it creates, use `forEach` or `for-of`. More in my post here: [*Misusing `map`*](https://thenewtoys.dev/blog/2021/04/17/misusing-map/) – T.J. Crowder Jun 24 '21 at 08:09
  • 1
    @T.J.Crowder I didn't know there was a difference -- thanks so much for this! – bottledbrain Jun 25 '21 at 03:48

1 Answers1

0

Well, you could just quick-generate a combined array that had everything you need in it:

let combined = monthlySchoolTests.map(e => ({...e, teachers: schoolTeachers.filter(t => t.schoolId === e.schoolId)[0].teachers}))

const ObjectId = (v) => v; // for testing
let monthlySchoolTests = [{
  schoolId: ObjectId('804d8527f390ghz26e3426j6'),
  schoolName: "School1",
  schoolTests: [
    { testName: "Chemistry", count: 15 },
    { testName: "Music", count: 8 }
  ]
 },
 {
  schoolId: ObjectId('804ef074384b3d43f125ghs5'),
  schoolName: "School2",
  schoolTests: [
    { testName: "Math", count: 3 },
    { testName: "Physics", count: 12 },
    { testName: "Biology", count: 10 }
  ]
 }
]

let schoolTeachers = [{
  schoolId: ObjectId('804ef074384b3d43f125ghs5'),
  schoolName: "School2",
  teachers: [
    { name: "Michael", email: "michael@school2.edu" },
    { name: "Jane", count: "jane@school2.edu" },
    { name: "Lukas", count: "lukas@school2.edu" }
  ]
 },
 {
  schoolId: ObjectId('804d8527f390ghz26e3426j6'),
  schoolName: "School1",
  teachers: [
    { name: "Cleo", email: "cleo@school1.edu" },
    { name: "Celine", count: "celine@school1.edu" },
    { name: "Ike", count: "ike@school1.edu" }
  ]
 }
]

let combined = monthlySchoolTests.map(e => ({...e, teachers: schoolTeachers.filter(t => t.schoolId === e.schoolId)[0].teachers}))

console.log(combined)
Kinglish
  • 23,358
  • 3
  • 22
  • 43