Retrieve Data of an Index from Object Store Using Cursor
Learn how to fetch data from the index using the cursor.
We'll cover the following
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
Open the database connection.
Create the
readonly
transaction.Get the object store.
Use the
index()
method to get the index object from the object store.Use the
openCursor()
method on the index object to get the cursor.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.