Retrieve Data of an Index from Object Store Using Cursor

Learn how to fetch data from the index using the cursor.

As discussed earlier, index in IndexedDB allows us to retrieve data from an object store more efficiently than simply iterating over all the records in the object store. An index is a data structure that keeps track of the keys of the records in an object store. We can use an index to quickly find records that match a specific key or range of keys.

Steps to get data using index

  1. Open the database connection.

  2. Create the readonly transaction.

  3. Get the object store.

  4. Use the index() method to get the index object from the object store.

  5. Use the openCursor() method on the index object to get the cursor.

  6. Loop through the cursor to get the data.

Example

Let’s write an example demonstrating how to fetch data from the index of the object store.

Get hands-on with 1400+ tech skills courses.