JS Array

Last Updated: August 27, 2021

How to define a simple array in JS

Get the first and last element of the array

You can use the following code to get the first element of the array. It is the 0th element of the array

let first=response.data[0]

If you want to get the last element of the array you can use the following code

let first=response.data[response.data.length]

Array of objects

This is how you define the array of objects.

time_slots: [
                    {'date': '2021-10-12', '1': '8.00AM-9.00AM','2': '9.00AM-10.00AM'},
                    {'date': '2021-10-13', '1': '8.00AM-9.00AM'},
            ]

How do you traverse the array of the objects?