mirror of
https://github.com/HeyPuter/puter.git
synced 2026-09-10 15:25:49 +00:00
kv add array example (#3748)
This commit is contained in:
+26
-1
@@ -4,7 +4,7 @@ description: Add values to an existing key or nested path.
|
||||
platforms: [websites, apps, nodejs, workers]
|
||||
---
|
||||
|
||||
Add values to an existing key. When you pass an object, each key is treated as a path and the value is added at that path.
|
||||
Add values to an existing key. When you pass an array, its elements are appended to the array stored at the key. When you pass an object, each key is treated as a path and the value is added at that path.
|
||||
|
||||
## Syntax
|
||||
|
||||
@@ -23,6 +23,8 @@ The key to add values to.
|
||||
|
||||
The value to add to the key. Defaults to `1` when omitted.
|
||||
|
||||
An array is appended element by element, so wrap a single value in an array to append it as one element: `puter.kv.add('scores', [5])` appends `5`.
|
||||
|
||||
#### `pathAndValue` (Object) (optional)
|
||||
|
||||
An object where each key is a dot-separated path (for example, `"profile.tags"`) and each value is the value (or values) to add at that path.
|
||||
@@ -35,6 +37,29 @@ Returns a `Promise` that resolves to the updated value stored at `key`.
|
||||
|
||||
## Examples
|
||||
|
||||
<strong class="example-title">Append to an array stored at a key</strong>
|
||||
|
||||
```html;kv-add-array
|
||||
<html>
|
||||
<body>
|
||||
<script src="https://js.puter.com/v2/"></script>
|
||||
<script>
|
||||
(async () => {
|
||||
await puter.kv.set('scores', [1, 2, 3, 4]);
|
||||
|
||||
// Each element of the array you pass is appended
|
||||
const updated = await puter.kv.add('scores', [5]);
|
||||
puter.print(`Updated scores: ${JSON.stringify(updated)}<br>`);
|
||||
|
||||
// Passing several values appends all of them
|
||||
const extended = await puter.kv.add('scores', [6, 7]);
|
||||
puter.print(`Extended scores: ${JSON.stringify(extended)}`);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
```
|
||||
|
||||
<strong class="example-title">Add values to an array inside an object</strong>
|
||||
|
||||
```html;kv-add
|
||||
|
||||
@@ -449,6 +449,12 @@ const examples = [
|
||||
slug: 'kv-add',
|
||||
source: '/playground/examples/kv-add.html',
|
||||
},
|
||||
{
|
||||
title: 'Add (Array value)',
|
||||
description: 'Append values to an array stored at a key with Puter.js key-value API. Run and experiment with this example in the playground.',
|
||||
slug: 'kv-add-array',
|
||||
source: '/playground/examples/kv-add-array.html',
|
||||
},
|
||||
{
|
||||
title: 'Remove',
|
||||
description: 'Remove values by path with Puter.js key-value API. Run and modify this code example instantly in your browser.',
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
<html>
|
||||
<body>
|
||||
<script src="https://js.puter.com/v2/"></script>
|
||||
<script>
|
||||
(async () => {
|
||||
await puter.kv.set('scores', [1, 2, 3, 4]);
|
||||
|
||||
// Each element of the array you pass is appended
|
||||
const updated = await puter.kv.add('scores', [5]);
|
||||
puter.print(`Updated scores: ${JSON.stringify(updated)}<br>`);
|
||||
|
||||
// Passing several values appends all of them
|
||||
const extended = await puter.kv.add('scores', [6, 7]);
|
||||
puter.print(`Extended scores: ${JSON.stringify(extended)}`);
|
||||
})();
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user