This repository was archived by the owner on Sep 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Iteration
Cédric Belin edited this page Aug 24, 2024
·
6 revisions
The Storage class is iterable: it implements the KeyValueIterable<String, String> protocol.
You can go through all key/value pairs contained using a for loop:
import webstorage.Storage;
function main() {
final localStorage = Storage.local();
localStorage.set("foo", "bar");
localStorage.set("bar", "baz");
localStorage.set("baz", "qux");
for (key => value in localStorage) {
trace('$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 webstorage.Storage;
function main() {
final sessionStorage = Storage.session();
sessionStorage.set("foo", "bar");
sessionStorage.set("prefix:bar", "baz");
final prefixedStorage = Storage.session({keyPrefix: "prefix:"});
prefixedStorage.set("baz", "qux");
for (key => value in prefixedStorage) {
trace('$key => $value');
// Round 1: "bar => baz"
// Round 2: "baz => qux"
}
}Note
The prefix is stripped from the keys returned by the iteration.