Powered By GitBook
SET_ITEM
Replaces a matching item in list

Description

Replaces or updates the first item from a list that matches the given criteria with a new item. Returns a list where the matching item has been changed to the new value.
The criteria is defined as a formula at the 2nd parameter. The formula is evaluated with each item in the list until a matching item is found. The search formula must result in a true/false value. If true, then item will be excluded from the result, otherwise it will be included.
The 3rd parameter defines either the replacement value for the item, or a formula that can use the existing, matching item resulting in a new value. For example, the formula can use SET\_KEY function to update a property of a matching item to a new value.
Note that only first matching item is replaced; if multiple items matches the criteria, then only first one is changed. If no matching item is found, then the original list is returned unchanged.

Function signature

1
SET_ITEM(list: Array, searchformula: Function, replacementformula: Function) => Array
Copied!
Parameter
Description
List
List of items
Search formula
Formula to be evaluated for each item, deciding whether the item is the one to remove
Replacement formula
A new item replacing the matching item, or a formula to be evaluated using the matching item for the replacement value

Examples

1
animals = [
2
{
3
"name": "dog",
4
"weight": 123
5
},
6
{
7
"name": "cat",
8
"weight": 48
9
},
10
{
11
"name": "rat",
12
"weight": 10
13
}
14
]
15
products = [
16
{
17
"name": "Duct tape",
18
"price": 2.9
19
},
20
{
21
"name": "Swiss army knife",
22
"price": 23
23
},
24
{
25
"name": "Glue",
26
"price": 1.5
27
}
28
]
Copied!
Formula
Return value
SET_ITEM(animals, item.name == "cat", { name: "horse", weight: 600 })
[{"name":"dog","weight":123},{"name":"horse","weight":600},{"name":"rat","weight":10}]
SET_ITEM(animals, item.name == "cat", SET_KEY(item, "weight", 23))
[{"name":"dog","weight":123},{"name":"cat","weight":23},{"name":"rat","weight":10}]
SET_ITEM(animals, index == 1, SET_KEY(item, "name", "horse"))
[{"name":"dog","weight":123},{"name":"horse","weight":48},{"name":"rat","weight":10}]
SET_ITEM<product>(products, product.name == "Swiss army knife", SET_KEY(product, "price", product.price * 2))
[{"name":"Duct tape","price":2.9},{"name":"Swiss army knife","price":46},{"name":"Glue","price":1.5}]
SET_ITEM<letter, position>(["a", "b", "c", "d", "e"], position == 2, UPPERCASE(letter) + "!")
["a","b","C!","d","e"]
Last modified 10mo ago
Export as PDF
Copy link