Pluxbox Logo

Merge array block

Purpose

Merge two lists into one list.

Input

  • Base list
    The base list you wish to expand with the patch list.

  • Patch list
    The list of items to be merged into the base list.

  • Key Path list (Optional)
    Where to find the unique identifier, aka key, on the objects in the list. Will not trigger any output.
    OBS! Will only merge the patch item into the list, if the item has this path for the key. If the value at the key path matches the value on a base item, the whole item base list will be overwritten by the patch item.

Output

  • Val
    A new list containing items from both the base list and patch list.

Example

I wish to combine these two lists of famous pokemons, since I won the battle and my opponent's pokemons are therefore mine. I don't want to have duplicates, so if my opponent has a pokemon with the same name as one of mine, I'll replace my pokemon.

My pokemons:

    [
      {
        "name":"Bulbasaur",
        "level":58,
        "moves":4
      },
      {
        "name":"Ditto",
        "level":99,
        "moves":1
      },
      {
        "name":"Rattata",
        "level":2,
        "moves":1
      },
      {
        "name":"wigglytuff",
        "level":69,
        "moves":6
      }
    ]

My opponent's pokemons:

    [
      {
        "name":"Pidgeot",
        "level":12,
        "moves":2
      },
      {
        "name":"Rattata",
        "level":15,
        "moves":3
      },
      {
        "name":"Dodrio",
        "level":13,
        "moves":1
      }
    ]

I can merge them into one list with the following logic:

graph LR
A([my pokemons]) --Base--> I(Merge array)
B([opponents pokemons]) --Patch--> I
C(['name']) --Key Path--> I
I --Value--> D([combined list])

That gives me a new combined list of pokemon with no duplicates:

    [
      {
        "name":"Bulbasaur",
        "level":58,
        "moves":4
      },
      {
        "name":"Ditto",
        "level":99,
        "moves":1
      },
      {
        "name":"wigglytuff",
        "level":69,
        "moves":6
      },
      {
        "name":"Pidgeot",
        "level":12,
        "moves":2
      },
      {
        "name":"Rattata",
        "level":15,
        "moves":3
      },
      {
        "name":"Dodrio",
        "level":13,
        "moves":1
      }
    ]

Caveats

  • If the key path is set and the patch list contains items without the key path, the last item without the key, will still be added to the list.
  • The newest items will be added at the bottom of the list. If the order is important, connect the output to the 'sort array' block.
  • There's currently no way to decide which item to keep during a conflict. The patch will always win.