JS:Cannot assign to ready only property to 'enrolledCourse' of object , I want to modify the parent array of object by removing matched coursename

Viewed 12

How do I remove the object in my array enrolledCourse that is nested inside another array where the courseName property of an object is = to esi. I'm trying to get the whole parent array back without that enrolledCourse removed.

// eslint-disable-next-line no-unused-expressions


const students = [
{
 name: "anand",`enter code here`
 uid: "BjaR2gC0HhT2K0KemEuVShQmqXB2",
 profilePic: "",
 password: "12345678",
 active: true,
 email: "anand@gmail.com",
 contactNo: "8987676589",
 createdAt: "2022-09-16T07:59:48.060Z",
 role: "student",
 enrolledCourse: [
   {
     courseName: "esi",
     assignmentDetails: [
       {
         documentUrl:"https://firebasestorage.googleapis.com/",
         assignmentDownloadedUrl: "",
         totalMarks: "",
         topicName: "one",
         assignmentDownloadedDate: "",
         isAssignmentUploaded: false,
         receivedMarks: "",
         assignmentUploadedUrl: "",
         assignmentUploadedDate: "",
         isAssignmentDownloaded: true,
       },
       {
         isAssignmentUploaded: false,
         topicName: "four",
         assignmentDownloadedDate: "",
         receivedMarks: "",
         assignmentUploadedDate: "",
         isAssignmentDownloaded: false,
         assignmentDownloadedUrl: "",
         documentUrl: "",
         totalMarks: "",
         assignmentUploadedUrl: "",
       },
     ],
   },
   {
     courseName: "pf",
     assignmentDetails: [
       {
         topicName: "one",
         assignmentDownloadedDate: "",
         receivedMarks: "",
         assignmentUploadedUrl: "",
         isAssignmentUploaded: false,
         assignmentUploadedDate: "",
         assignmentDownloadedUrl: "",
         isAssignmentDownloaded: false,
         totalMarks: "",
         documentUrl:"https://firebasestorage.googleapis.com/",
       },
     ],
   },
   {
     courseName: "student",
     assignmentDetails: [
       {
         assignmentUploadedUrl: "",
         documentUrl: "",
         receivedMarks: "",
         assignmentUploadedDate: "",
         assignmentDownloadedDate: "",
         isAssignmentUploaded: false,
         topicName: "one",
         assignmentDownloadedUrl: "",
         isAssignmentDownloaded: false,
         totalMarks: "",
       },
     ],
   },
 ],
},
{
 contactNo: "9876545434",
 createdAt: "2022-09-20T07:53:38.903Z",
 password: "12345678",
 email: "gopal@gmail.com",
 uid: "GOgUuKQV41hzau9zGPl0HViYp543",
 name: "gopal",
 profilePic: "",
 role: "student",
 active: true,
 enrolledCourse: [
   {
     courseName: "esi",
     assignmentDetails: [
       {
         assignmentDownloadedDate: "",
         isChecked: false,
         assignmentUploadedUrl: "",
         isAssignmentDownloaded: false,
         totalMarks: 10,
         topicName: "one",
         assignmentUploadedDate: "",
         receivedMarks: "",
         assignmentDownloadedUrl: "",
         isAssignmentUploaded: false,
         documentUrl: "",
       },
     ],
   },
 ],
},
{
 uid: "LbaCxv6PPBWJh4EAf2JeGB3nwzB3",
 profilePic: "",
 email: "bholu@gmail.com",
 role: "student",
 active: true,
 contactNo: "8909876765",
 password: "12345678",
 name: "bholu",
 createdAt: "2022-09-20T10:28:26.731Z",
 enrolledCourse: [
   {
     courseName: "bank",
     assignmentDetails: [
       {
         receivedMarks: "",
         assignmentUploadedDate: "",
         isAssignmentUploaded: false,
         isAssignmentDownloaded: false,
         assignmentUploadedUrl: "",
         documentUrl:"https://firebasestorage.googleapis.com/",
         totalMarks: "80",
         assignmentDownloadedDate: "",
         assignmentDownloadedUrl: "",
         topicName: "one",
         isChecked: false,
       },
       null,
       null,
     ],
   },
 ],
},
{
 active: true,
 email: "rohit2@gmail.com",
 uid: "Lw2rbXrwLWgUUr3tfaaSHTIoOGj2",
 createdAt: "2022-09-20T10:39:21.630Z",
 contactNo: "9098787656",
 name: "rohit",
 role: "student",
 password: "12345678",
 profilePic: "",
 enrolledCourse: [
   {
     courseName: "bank",
     assignmentDetails: [
       {
         isAssignmentUploaded: false,
         isAssignmentDownloaded: false,
         assignmentUploadedUrl: "",
         assignmentDownloadedDate: "",
         isChecked: false,
         assignmentDownloadedUrl: "",
         documentUrl:"https://firebasestorage.googleapis.com/",
         assignmentUploadedDate: "",
         topicName: "one",
         receivedMarks: "",
         totalMarks: "80",
       },
       null,
       null,
     ],
   },
 ],
},
];

I am using this code, but getting mentiond error
guys please help me out. Thanks!

students.forEach(function (o) {
  o.enrolledCourse = o.enrolledCourse.filter((s) => s.courseName !== "esi");
});

I need to get

// eslint-disable-next-line no-unused-expressions

[
  {
    name: "anand",
    uid: "BjaR2gC0HhT2K0KemEuVShQmqXB2",
    profilePic: "",
    password: "12345678",
    active: true,
    email: "anand@gmail.com",
    contactNo: "8987676589",
    createdAt: "2022-09-16T07:59:48.060Z",
    role: "student",

    enrolledCourse: [
      
      {
        courseName: "pf",
        assignmentDetails: [
          {
            topicName: "one",
            assignmentDownloadedDate: "",
            receivedMarks: "",
            assignmentUploadedUrl: "",
            isAssignmentUploaded: false,
            assignmentUploadedDate: "",
            assignmentDownloadedUrl: "",
            isAssignmentDownloaded: false,
            totalMarks: "",
            documentUrl:"https://firebasestorage.googleapis.com/",
          },
        ],
      },
      {
        courseName: "student",
        assignmentDetails: [
          {
            assignmentUploadedUrl: "",
            documentUrl: "",
            receivedMarks: "",
            assignmentUploadedDate: "",
            assignmentDownloadedDate: "",
            isAssignmentUploaded: false,
            topicName: "one",
            assignmentDownloadedUrl: "",
            isAssignmentDownloaded: false,
            totalMarks: "",
          },
        ],
      },
    ],
  },
  {
    contactNo: "9876545434",
    createdAt: "2022-09-20T07:53:38.903Z",
    password: "12345678",
    email: "gopal@gmail.com",
    uid: "GOgUuKQV41hzau9zGPl0HViYp543",
    name: "gopal",
    profilePic: "",
    role: "student",
    active: true,
    enrolledCourse: [
      
    ],
  },
  {
    uid: "LbaCxv6PPBWJh4EAf2JeGB3nwzB3",
    profilePic: "",
    email: "bholu@gmail.com",
    role: "student",
    active: true,
    contactNo: "8909876765",
    password: "12345678",
    name: "bholu",
    createdAt: "2022-09-20T10:28:26.731Z",
    enrolledCourse: [
      {
        courseName: "bank",
        assignmentDetails: [
          {
            receivedMarks: "",
            assignmentUploadedDate: "",
            isAssignmentUploaded: false,
            isAssignmentDownloaded: false,
            assignmentUploadedUrl: "",
            documentUrl:"https://firebasestorage.googleapis.com/",
            totalMarks: "80",
            assignmentDownloadedDate: "",
            assignmentDownloadedUrl: "",
            topicName: "one",
            isChecked: false,
          },
          null,
          null,
        ],
      },
    ],
  },
  {
    active: true,
    email: "rohit2@gmail.com",
    uid: "Lw2rbXrwLWgUUr3tfaaSHTIoOGj2",
    createdAt: "2022-09-20T10:39:21.630Z",
    contactNo: "9098787656",
    name: "rohit",
    role: "student",
    password: "12345678",
    profilePic: "",
    enrolledCourse: [
      {
        courseName: "bank",
        assignmentDetails: [
          {
            isAssignmentUploaded: false,
            isAssignmentDownloaded: false,
            assignmentUploadedUrl: "",
            assignmentDownloadedDate: "",
            isChecked: false,
            assignmentDownloadedUrl: "",
            documentUrl:"https://firebasestorage.googleapis.com/",
            assignmentUploadedDate: "",
            topicName: "one",
            receivedMarks: "",
            totalMarks: "80",
          },
          null,
          null,
        ],
      },
    ],
  },
];

0 Answers
Related