Objects
Object Manipulation Functions
PenScript provides powerful tools for manipulating objects, allowing you to transform and update data dynamically. Below are two key functions for working with objects.
:assign
:assign
The :assign
function allows you to overwrite the keys of an object using a list of objects. When multiple objects are provided, the values from later objects will overwrite the corresponding keys in earlier objects.
Syntax:
{
":assign": [object1, object2, ...]
}
Example:
{
":assign": [
{
"key1": "foo",
"key2": "bar"
},
{
"key2": "foo2"
}
]
}
Result:
{
"key1": "foo",
"key2": "foo2"
}
Explanation: In this example, key2 in the second object overwrites the value of key2 in the first object.
Use Case: Use :assign to merge objects or update specific keys with new values dynamically.
:object-entries
The :object-entries
function transforms an object into a list of entries, with each entry containing a key
and a value
. It also supports optional arguments for customization.
Syntax:
{
":object-entries": object,
":as": {
"key": expression_for_key,
"value": expression_for_value
},
":order-by": "key" | "value" (optional)
}
Example:
{
"variables": [
{
"test_object": {
"gender": "female",
"given_name": "Olivia",
"family_name": "De Smet"
}
},
{
"test2": {
":object-entries": "{user}",
":as": {
"key": "{@value}",
"value": "{@key}"
}
},
"test": {
":object-entries": "{user}",
":as": "{@value}"
}
}
]
}
Results:
{
"test2": [
{
"key": "De Smet",
"value": "family_name"
},
{
"key": "female",
"value": "gender"
},
{
"key": "Olivia",
"value": "given_name"
}
],
"test": [
"De Smet",
"female",
"Olivia"
]
}
Key Features:
:as
: Specifies how to mapkey
andvalue
for each entry in the output list.:order-by
(optional): Sort the entries bykey
orvalue
.
Use Cases:
- Transform objects into lists for easier iteration or processing.
- Extract specific keys or values dynamically.
- Reorganize or filter object data.
These object manipulation functions provide flexible tools to handle and transform objects, making it easier to integrate dynamic behaviors into your workflows.
Updated 6 days ago