Skip to content

remove vague addition store setter filter #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/routes/concepts/stores.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -347,15 +347,21 @@ setStore("users", 3, "loggedIn" , (loggedIn) => !loggedIn)

### Filtering values

In scenarios where you want to update elements in an array based on a specific condition, you can pass a function as an argument.
This function will act as a filter, allowing you to select elements that satisfy the condition.
It receives the old value and index as arguments, providing the flexibility to make conditional updates.
To update elements in an array based on specific conditions, you can pass a function as an argument.
This function will act as a filter, It receives the old value and index, giving you flexibility to apply logic that targets specific cases.
This might include using methods like `.startsWith()`, `includes()`, or other comparison techniques to determine which elements should be updated.

```jsx
// update users with username that starts with "t"
setStore("users", (user) => user.username.startsWith("t"), "loggedIn", false)
```

In addition to [`.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith), you can use other array methods like [`.find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) to filter for the values that you need.
// update users with location "Canada"
setStore("users", (user) => user.location == "Canada" , "loggedIn", false)

// update users with id 1, 2 or 3
let ids = [1,2,3]
setStore("users", (user) => ids.includes(user.id) , "loggedIn", false)
```

## Modifying objects

Expand Down