1

I'm trying to return results to frontend from backend but would like to remove some of the data taken from the query.

I'm currently able to remove it for the first object in the array but would like to do it for every object.

const { url, booking_created, booking_date, student_id, student_name, ...sendEvent} = bookedSlots[0]

sending sendEvent returns

{
    "id": 1,
    "start": "2022-09-28T19:00:00.000Z",
    "end": "2022-09-28T20:00:00.000Z",
    "duration": "60MIN",
    "title": "1 hour lao lessons",
    "tutor_id": "901"
}

I'm not sure how to loop through the object and apply this to everything.

{
    "0": {
        "id": 1,
        "booking_created": "2022-09-24T19:49:00.000Z",
        "booking_date": "2022-09-24T00:00:00.000Z",
        "start": "2022-09-28T19:00:00.000Z",
        "end": "2022-09-28T20:00:00.000Z",
        "duration": "60MIN",
        "student_id": 1,
        "student_name": "Bob the builder",
        "title": "1 hour lessons",
        "url": "nan",
        "tutor_id": "901"
    },
    "1": {
        "id": 2,
        "booking_created": "2022-09-24T20:25:15.000Z",
        "booking_date": "2022-09-24T00:00:00.000Z",
        "start": "2022-09-28T21:00:00.000Z",
        "end": "2022-09-28T22:00:00.000Z",
        "duration": "60MIN",
        "student_id": 1,
        "student_name": "Big boss 1",
        "title": "1 hour lessons",
        "url": "nan",
        "tutor_id": "901"
    },
    "2": {
        "id": 3,
        "booking_created": "2022-09-28T12:00:13.000Z",
        "booking_date": "2022-09-08T00:00:00.000Z",
        "start": "2022-09-28T18:00:00.000Z",
        "end": "2022-09-28T19:00:00.000Z",
        "duration": "60MIN",
        "student_id": 1,
        "student_name": "bobo",
        "title": "bob lesson",
        "url": "123",
        "tutor_id": "901"
    }
}
DMantas
  • 125
  • 2
  • 10
  • In your last code block, that is one large object and not an array is that correct? – Dale Sep 28 '22 at 13:31
  • 1
    If you want an array of objects with that subset of properties, do what you're doing, but in a loop -- probably via `map`: `const sendEvents = bookedSlots.map(({url, booking_created, booking_date, student_id, student_name, ...sendEvent}) => sendEvent);` See the answers to the linked questions for more details. – T.J. Crowder Sep 28 '22 at 13:34
  • This worked perfectly, first time I've seen .map thank you very much. I've seen many examples of forEach but can't seem to fully understand it. – DMantas Sep 28 '22 at 13:37

0 Answers0