Skip to content
Advertisement

Iterate over Store via Map

Via a Map Objects are added to a Svelte Store.

Every object has an ID and it should be possible to iterate over those IDs. With $entities.get(“123”).currentPage;

The current Page of this Object is returned.

How can I iterate over more than one ID such that the current pages of all objects in the store are returned?

Advertisement

Answer

You could take those IDs into an array, and then map that array to the code you listed in your question. For example:

function getPages(ids) {
    return ids.map(id => $entitites.get(id).currentPage);
}

// example call:
let pages = getPages(["123", "146", "195"]);
8 People found this is helpful
Advertisement