# Adding new calculated field to an object inside an array (foreach)

**URL:** <https://discuss.elastic.co/t/adding-new-calculated-field-to-an-object-inside-an-array-foreach/127947>\
**Category:** Elasticsearch\
**Created:** [April 13, 2018, 9:30am UTC](https://discuss.elastic.co/t/adding-new-calculated-field-to-an-object-inside-an-array-foreach/127947 "2018-04-13T09:30:26Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [April 13, 2018, 9:30am UTC](https://discuss.elastic.co/t/adding-new-calculated-field-to-an-object-inside-an-array-foreach/127947/1 "2018-04-13T09:30:26Z")

</div>

My first post here. So please bear with me.

## Sample document:

```
{
  "persons" : [
    {
      "id" : "1",
      "name" : "John Doe"
    },
    {
      "id" : "2",
      "name" : "Jane Doe"
    }
  ]
}

```

I would like to add a new field named "calcValue" to each person object as follows:  
calcValue = id + '#' + name

* * *

Tried foreach processor but getting concurrent modification exception.

```
"processors": [
    {
      "foreach": {
        "field": "persons",
        "processor": {
          "script": {
            "source": "ctx.persons.add(params.calcValue)",
            "params": {
              "calcValue": "_ingest._value.id+'#'+_ingest._value.name"
            }
          }
        }
      }
    }

```

* * *

I understand why ConcurrentModificationException was raised but I am unable to get this working. Please can someone help.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 13, 2018, 1:05pm UTC](https://discuss.elastic.co/t/adding-new-calculated-field-to-an-object-inside-an-array-foreach/127947/2 "2018-04-13T13:05:13Z")

</div>

I'd do something like this:

```auto
POST _ingest/pipeline/_simulate
{
  "docs": [
    {
      "_id": "foo",
      "_source": {
        "persons": [
          {
            "id": "1",
            "name": "John Doe"
          },
          {
            "id": "2",
            "name": "Jane Doe"
          }
        ]
      }
    }
  ],
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": """
            int total = 0;
            for (int i = 0; i < ctx['persons'].length; ++i) {
              String text = ctx['persons'][i]['id']+"#"+ctx['persons'][i]['name'];
              ctx['persons'][i]['calcValue']=text;
            }
"""
        }
      }
    ]
  }
}

```

It gives:

```auto
{
  "docs": [
    {
      "doc": {
        "_index": "_index",
        "_type": "_type",
        "_id": "foo",
        "_source": {
          "persons": [
            {
              "name": "John Doe",
              "id": "1",
              "calcValue": "1#John Doe"
            },
            {
              "name": "Jane Doe",
              "id": "2",
              "calcValue": "2#Jane Doe"
            }
          ]
        },
        "_ingest": {
          "timestamp": "2018-04-13T13:04:46.213Z"
        }
      }
    }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![LGans316](https://avatars.discourse-cdn.com/v4/letter/l/cab0a1/32.png) [@LGans316](https://discuss.elastic.co/u/LGans316)\
**Post date:** [April 13, 2018, 1:55pm UTC](https://discuss.elastic.co/t/adding-new-calculated-field-to-an-object-inside-an-array-foreach/127947/3 "2018-04-13T13:55:15Z")

</div>

Thanks a lot Sir. You are a legend. 😍

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2018, 1:55pm UTC](https://discuss.elastic.co/t/adding-new-calculated-field-to-an-object-inside-an-array-foreach/127947/4 "2018-05-11T13:55:19Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
