# How to add field to every element from an array with variable length

**URL:** <https://discuss.elastic.co/t/how-to-add-field-to-every-element-from-an-array-with-variable-length/126003>\
**Category:** Logstash\
**Created:** [March 29, 2018, 1:41am UTC](https://discuss.elastic.co/t/how-to-add-field-to-every-element-from-an-array-with-variable-length/126003 "2018-03-29T01:41:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![terrymu](https://avatars.discourse-cdn.com/v4/letter/t/ea666f/32.png) [@terrymu](https://discuss.elastic.co/u/terrymu)\
**Post date:** [March 29, 2018, 1:41am UTC](https://discuss.elastic.co/t/how-to-add-field-to-every-element-from-an-array-with-variable-length/126003/1 "2018-03-29T01:41:12Z")

</div>

Hi experts,

I have an array such like this:  
`"NODE" : ["node1", "node2", "node3"]`

And my first requirement is add field to every element from this array. I have used a hard code to add three fields for these three elements.

```
mutate {
                add_field => {
                                "container1" => "%{[NODE][1]}"
                                "container2" => "%{[NODE][2]}"
                                "container3" => "%{[NODE][3]}"
                }
}

```

Then I can use them to do my retrieve work.  
Now my major question is coming: actually the length of this array is variable! sometimes it may include more than 10 elements, that means I need wirte more than 10 line's redundancy code in my filter pattern. Are there some methods that can handle the variable length array that make the pattern code efficiently?

Thank you.

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [March 30, 2018, 5:12pm UTC](https://discuss.elastic.co/t/how-to-add-field-to-every-element-from-an-array-with-variable-length/126003/2 "2018-03-30T17:12:21Z")

</div>

```auto
ruby { code => ' event.get("NODE").each_with_index { |x, i| event.set( "container#{i+1}" , x) } ' }

```

---

<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:** [April 27, 2018, 5:12pm UTC](https://discuss.elastic.co/t/how-to-add-field-to-every-element-from-an-array-with-variable-length/126003/3 "2018-04-27T17:12:24Z")

</div>

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