# Manipulating and iterating over arrays

**URL:** <https://forum.alumio.com/t/manipulating-and-iterating-over-arrays/611>\
**Category:** General\
**Tags:** data-transformer\
**Created:** [May 25, 2023, 7:07am UTC](https://forum.alumio.com/t/manipulating-and-iterating-over-arrays/611 "2023-05-25T07:07:11Z")\
**Posts on this page:** 1\
**Showing post:** 2

<div class="post-metadata">

**Author:** ![Gugi](https://forum.alumio.com/user_avatar/forum.alumio.com/gugi/32/236_2.png) [@Gugi](https://forum.alumio.com/u/Gugi)\
**Post date:** [May 26, 2023, 10:53am UTC](https://forum.alumio.com/t/manipulating-and-iterating-over-arrays/611/2 "2023-05-26T10:53:49Z")

</div>

Hi @kjetil

Thank you for your thorough explanation of your case. You are right that you can do the approach you mentioned. We will also introduce the way to do it using [Group Records](https://forum.alumio.com/t/how-to-group-records/279) and [Operator Transformer](https://forum.alumio.com/t/operator-transformer/286).

Since we depend on the `propId` properties to define the method by comparing them between the `existingOptions` and `newOptions` arrays, we can group the objects of each array and set the `propId` as the key of the grouped objects.

 ![image](https://alumio-forum.s3.dualstack.eu-central-1.amazonaws.com/uploads/original/1X/45d64162442a6e4ca8545ced07edb443e146772a.png)

We can do the same with the `newOptions` array.

This way, we will have `propId`-grouped objects of `existingOptions` and `newOptions`, such as below.

```auto
  "existingOptions": {
    "1": [
      {
        "propId": "1",
        "optionId": "2"
      }
    ],
    "2": [
      {
        "propId": "2",
        "optionId": "34"
      }
    ]
  },
  "newOptions": {
    "1": [
      {
        "propId": "1",
        "optionId": "2"
      }
    ],
    "3": [
      {
        "propId": "3",
        "optionId": "5"
      }
    ]
  }
}

```

Next, we will use Operator Transformer to map which objects need to be created, updated, or deleted.

In order to get the objects we need to create (the `POST` method), we should use “Array Diff Keys” operator, set the `newOptions` as the left path and `existingOptions` as the right path. We can set the Destination to a property, such as `call.POST`. Please see the below table.

```auto
| Method | Operator | Left Path | Right Path | Destination |
|---------|----------------------|-----------------|-----------------|-------------|
| POST | Array Diff Keys | newOptions | existingOptions | call.POST |
| PUT | Array Intersect Keys | newOptions | existingOptions | call.PUT |
| DELETE | Array Diff Keys | existingOptions | newOptions | call.DELETE |

```

We will have the below data as the result of the above Operator Transformers.

```auto
{
  "call": {
    "POST": {
      "3": [
        {
          "propId": "3",
          "optionId": "5"
        }
      ]
    },
    "PUT": {
      "1": [
        {
          "propId": "1",
          "optionId": "2"
        }
      ]
    },
    "DELETE": {
      "2": [
        {
          "propId": "2",
          "optionId": "34"
        }
      ]
    }
  }
}

```

Then, we should map the method, which is a key at this moment, into a property using [Move data between accessors](https://forum.alumio.com/t/move-data-between-accessors/284). The Source accessor should be “Key accessor” and the Root is “call”. The Destination accessor should be “Structure accessor” with “method” as the Key, “properties” as the Value, and “call” the “Root”. The result of the transformer will be such below.

```auto
{
  "call": [
    {
      "method": "POST",
      "properties": {
        "3": [
          {
            "propId": "3",
            "optionId": "5"
          }
        ]
      }
    },
    {
      "method": "PUT",
      "properties": {
        "1": [
          {
            "propId": "1",
            "optionId": "2"
          }
        ]
      }
    },
    {
      "method": "DELETE",
      "properties": {
        "2": [
          {
            "propId": "2",
            "optionId": "34"
          }
        ]
      }
    }
  ]
}

```

We can then copy the `id` property into each object in pattern `call.*.properties.*.*` using the [Recursively copy values to children](https://forum.alumio.com/t/recursively-copy-values-to-children/287).

In order to copy the method, we should use a [Node, transform nodes](https://forum.alumio.com/t/node-transformer/348) entity transformer using the pattern `call.*`. Inside the node, we can use Recursively copy values to children to copy the `method` property into each object using pattern `properties.*.*`. The entity will be such below.

```auto
{
  "call": [
    {
      "method": "POST",
      "properties": {
        "3": [
          {
            "propId": "3",
            "optionId": "5",
            "id": "1",
            "method": "POST"
          }
        ]
      }
    },
    {
      "method": "PUT",
      "properties": {
        "1": [
          {
            "propId": "1",
            "optionId": "2",
            "id": "1",
            "method": "PUT"
          }
        ]
      }
    },
    {
      "method": "DELETE",
      "properties": {
        "2": [
          {
            "propId": "2",
            "optionId": "34",
            "id": "1",
            "method": "DELETE"
          }
        ]
      }
    }
  ]
}

```

Finally, we can use a new [Node, transform nodes](https://forum.alumio.com/t/node-transformer/348) entity transformer using the pattern `call.*.properties.*.*` and submit the HTTP request using HTTP transformer there.

I hope my explanation is clear to you. It may be not so short solution, but this is an alternative way to achieve your objective. Feel free to let me know if you have any questions.

---

_[View the full topic](https://forum.alumio.com/t/manipulating-and-iterating-over-arrays/611)._
