Created
January 5, 2024 21:38
-
-
Save emsearcy/938ee1ae87c689a79c9d3125a0e69131 to your computer and use it in GitHub Desktop.
Lowdefy Snippet for Merging Object Arrays
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Array merge takes a source array of objects and merges into into a | |
# destination array of objects, while looking up an defined attribute from each | |
# object (in both arrays) to de-duplicate the final array. | |
# | |
# So if you call with: | |
# _ref: | |
# path: path/to/array_merge.yaml | |
# vars: | |
# from: [ {"id": 1, name: "Alice"}, {"id": 2, name: "Bob"} ] | |
# into: [ {"id": 2, name: "Robert"}, {"id": 3, name: "Charlie"} ] | |
# key: id | |
# | |
# ... it will resolve to: | |
# | |
# [ {"id": 1, name: "Alice"}, {"id": 2, name: "Robert"}, {"id": 3, name: Charlie} ] | |
# | |
# ... noting that the attributes of 2 took precedence from the values of "into". | |
--- | |
# Convert {"id1": {on_key: "id1", attr: "a"}} to [{on_key: "id1", attr: "a"}]. | |
_object.values: | |
# Deduplicate-merge objects {"id1": {on_key: "id1", attr: "a"}} into {"id1": {on_key: "id1", attr: "a"}}. | |
_object.assign: | |
# Convert [ ["id1", {on_key: "id1", attr: "a"} ] ] to {"id1": {on_key: "id1", attr: "a"}} | |
- _object.fromEntries: | |
# Convert [ {on_key: "id1", attr: "a"} ] into [ ["id1", {on_key: "id1", attr: "a"} ] ] | |
_array.map: | |
'on': | |
_var: into | |
callback: | |
# Convert {on_key: "id1", attr: "a"} into ["id1", {on_key: "id1", attr: "a"} ] | |
_function: | |
- __get: | |
# Get the *value* of the primary key (passed as the 'on' | |
# var). | |
from: | |
__args: 0 | |
key: | |
_var: key | |
- __args: 0 | |
# Same exact process but with the "from" var. | |
- _object.fromEntries: | |
_array.map: | |
'on': | |
_var: from | |
callback: | |
_function: | |
- __get: | |
from: | |
__args: 0 | |
key: | |
_var: key | |
- __args: 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment