Skip to content

Iteration

Cédric Belin edited this page Jun 16, 2024 · 8 revisions

The Storage class is iterable: it implements the Symbol.iterator function. You can go through all key/value pairs contained using a for...of loop:

import {Storage} from "@cedx/webstorage";

const localStorage = Storage.local()
  .set("foo", "bar")
  .set("bar", "baz")
  .set("baz", "qux");

for (const [key, value] of localStorage) {
  console.log(`${key} => ${value}`);
  // Round 1: "foo => bar"
  // Round 2: "bar => baz"
  // Round 3: "baz => qux"
}

Warning

The order of keys is user-agent defined, so you should not rely on it.

If you have configured the instance to use a key prefix, the iteration will only loop over the values that have that same key prefix:

import {Storage} from "@cedx/webstorage";

Storage.session()
  .set("foo", "bar")
  .set("prefix:bar", "baz");

const prefixedStorage = Storage.session({keyPrefix: "prefix:"})
  .set("baz", "qux");

for (const [key, value] of prefixedStorage) {
  console.log(`${key} => ${value}`);
  // Round 1: "bar => baz"
  // Round 2: "baz => qux"
}

Note

The prefix is stripped from the keys returned by the iteration.

Clone this wiki locally