# Extract string from list with a function node

**URL:** <https://discourse.nodered.org/t/extract-string-from-list-with-a-function-node/53967>\
**Category:** General\
**Tags:** function-node\
**Created:** [18 November 2021 19:54 UTC](https://discourse.nodered.org/t/extract-string-from-list-with-a-function-node/53967 "2021-11-18T19:54:43Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![giuliandenicola](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giuliandenicola/32/51288_2.png) [@giuliandenicola](https://discourse.nodered.org/u/giuliandenicola)\
**Post date:** [18 November 2021 19:54 UTC](https://discourse.nodered.org/t/extract-string-from-list-with-a-function-node/53967/1 "2021-11-18T19:54:43Z")

</div>

Hi,

I don't know how to search for this question that I have so I'll ask. If there already is a post about this let me know.

I would like to have a nodered setup were I can have a list of items and based on the number that is passed through it will send that list item in my payload. For example:

```auto
var list = [effect1, effect2, effect3, effect4]

if (msg.count === 1) {
    msg.payload = { "effect": "#list_item1" }
    return msg;
}

if (msg.count === 2) {
    msg.payload = { "effect": "#list_item2" }
    return msg;

}

if (msg.count === 3) {
    msg.payload = { "effect": "#list_item3" }
    return msg;

}

```

This will then passthrough a home assistant node that calls a service. For example:

```auto
[{"id":"023769838aa17645","type":"function","z":"ab6ef931.883208","name":"effect list","func":"\nvar list = [effect1, effect2, effect3, effect4]\n\nif (msg.count === 1) {\n msg.payload = { \"effect\": \"#list_item1\" }\n return msg;\n}\n\nif (msg.count === 2) {\n msg.payload = { \"effect\": \"#list_item2\" }\n return msg;\n\n}\n\nif (msg.count === 3) {\n msg.payload = { \"effect\": \"#list_item3\" }\n return msg;\n\n}","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":580,"wires":[["03a9de00a40cc8d0"]]},{"id":"bb0251f6073343ae","type":"function","z":"ab6ef931.883208","name":"Last -1","func":"msg.decrement = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":560,"wires":[["3911f3ded3b1c024"]]},{"id":"836e34c650d67da1","type":"function","z":"ab6ef931.883208","name":"Next +1","func":"msg.increment = 1;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":440,"y":600,"wires":[["3911f3ded3b1c024"]]},{"id":"3911f3ded3b1c024","type":"counter","z":"ab6ef931.883208","name":"","init":"0","step":1,"lower":null,"upper":null,"mode":"increment","outputs":"1","x":580,"y":580,"wires":[["023769838aa17645"]]},{"id":"5773db3243e9f741","type":"inject","z":"ab6ef931.883208","name":"-1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":280,"y":560,"wires":[["bb0251f6073343ae"]]},{"id":"c9f7b959181fc212","type":"inject","z":"ab6ef931.883208","name":"+1","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0","payloadType":"num","x":280,"y":600,"wires":[["836e34c650d67da1"]]},{"id":"03a9de00a40cc8d0","type":"api-call-service","z":"ab6ef931.883208","name":"change effect","server":"4fc31d47.aa1574","version":3,"debugenabled":false,"service_domain":"light","service":"turn_on","entityId":"light.ledstrip_keuken_wled","data":"{{ msg.payload }}","dataType":"jsonata","mergecontext":"","output_location":"payload","output_location_type":"msg","mustacheAltTags":false,"x":880,"y":580,"wires":[[]]},{"id":"4fc31d47.aa1574","type":"server","name":"Home Assistant","legacy":false,"addon":false,"rejectUnauthorizedCerts":true,"ha_boolean":"y|yes|true|on|home|open","connectionDelay":true,"cacheJson":true}]

```

If this is not clear for you I am sorry I do not know how to I can explain it more.  
Thanks in advance!

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [18 November 2021 20:29 UTC](https://discourse.nodered.org/t/extract-string-from-list-with-a-function-node/53967/2 "2021-11-18T20:29:06Z")

</div>

something like below maybe.

```auto
const list = ["effect1", "effect2", "effect3", "effect4"];
msg.payload = {"effect": list[msg.count -1]};
return msg;

```

msg.count has to be 1 to 4

---

<div class="post-metadata">

**Author:** ![giuliandenicola](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/giuliandenicola/32/51288_2.png) [@giuliandenicola](https://discourse.nodered.org/u/giuliandenicola)\
**Post date:** [22 November 2021 10:19 UTC](https://discourse.nodered.org/t/extract-string-from-list-with-a-function-node/53967/4 "2021-11-22T10:19:55Z")

</div>

That worked thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [6 December 2021 10:20 UTC](https://discourse.nodered.org/t/extract-string-from-list-with-a-function-node/53967/5 "2021-12-06T10:20:30Z")

</div>

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