# Creating a Lookup Table with double digits

**URL:** <https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623>\
**Category:** General\
**Created:** [18 August 2022 20:41 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623 "2022-08-18T20:41:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jaime\_G](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Jaime\_G](https://discourse.nodered.org/u/Jaime_G)\
**Post date:** [18 August 2022 20:41 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/1 "2022-08-18T20:41:26Z")

</div>

Hi community!

I've been using a look up function that I've created for some time now and it's been working well,,,, until today that I ran into a scenario that has double digits.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/8/784f7e7a7de08e52ee16ea95a53cd77413ba3e4d.png)

When I inject the value 1 through 9 everything works perfectly. When I start doing double digits I get a double replacement, see picture of the output of a debug node when I inject the value 1, 9, 11, 15.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/9/d965a55e62a3ea5854efa8b089fdbc540487bf0b.png)

When I inject 11 the output replaces two times the number 1.

Any suggestions on how to read and replace "11" versus "1 and 1" ?

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 August 2022 22:00 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/2 "2022-08-18T22:00:10Z")

</div>

re-write the function in reverse order will work, but it may not be the best solution.

Can you show us what is inside `msg.payload` immediately before this function?

---

<div class="post-metadata">

**Author:** ![Hopperpop](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hopperpop/32/42533_2.png) [@Hopperpop](https://discourse.nodered.org/u/Hopperpop)\
**Post date:** [18 August 2022 22:42 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/3 "2022-08-18T22:42:50Z")

</div>

You can make use of regular expressions to look if there is no digit before and after the match:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/5/f5cd81c2cd4920fbc7a9f33c944110aae5b85c37.png)

You can find a better explanation why it works here: [regex101](https://regex101.com/r/1mMUbf/1)

EDIT:  
Maybe it even better to use /(?\<!(\w))1(?!\w)/gi. As you also don't want to replace "L1" with anything, only numbers that aren't next to any word character: a-zA-Z0-9\_.

---

<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 August 2022 23:03 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/4 "2022-08-18T23:03:55Z")

</div>

If msg.payload coming into the function is a number, then you could just use a lookup array.  
e.g

```auto
[{"id":"3fea8003.98bac8","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"3","payloadType":"num","x":180,"y":120,"wires":[["88e2c287.49512"]]},{"id":"88e2c287.49512","type":"function","z":"30af2d3e.d94ea2","name":"","func":"const lookup = [\n null,\n \"L1\",\n \"L2\",\n \"L1-L2\",\n \"L3\",\n \"etc\"\n];\nmsg.payload = lookup[msg.payload] + \" Connected\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":340,"y":140,"wires":[["4e45a50.133c95c"]]},{"id":"bfac3654.beb8f","type":"inject","z":"30af2d3e.d94ea2","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"4","payloadType":"num","x":175.3333282470703,"y":157.33334350585938,"wires":[["88e2c287.49512"]]},{"id":"4e45a50.133c95c","type":"debug","z":"30af2d3e.d94ea2","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":560,"y":120,"wires":[]}]

```

```auto
const lookup = [
    null,
    "L1",
    "L2",
    "L1-L2",
    "L3",
    "etc"
];
msg.payload = lookup[msg.payload] + " Connected";
return msg;

```

---

<div class="post-metadata">

**Author:** ![Jaime\_G](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Jaime\_G](https://discourse.nodered.org/u/Jaime_G)\
**Post date:** [19 August 2022 00:03 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/5 "2022-08-19T00:03:20Z")

</div>

Thanks for this suggestion Steve, I did in fact try this out before reaching out to the community. It provided an unexpected result which caused me to seek advice from the pros.

A reply from Hopperpop did the trick 😊

---

<div class="post-metadata">

**Author:** ![Jaime\_G](https://avatars.discourse-cdn.com/v4/letter/j/e56c9b/32.png) [@Jaime\_G](https://discourse.nodered.org/u/Jaime_G)\
**Post date:** [19 August 2022 00:04 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/6 "2022-08-19T00:04:13Z")

</div>

This did the trick !

Many thanks to your suggestion Hopperpop!!!

This community is awesome!

---

<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:** [2 September 2022 00:05 UTC](https://discourse.nodered.org/t/creating-a-lookup-table-with-double-digits/66623/7 "2022-09-02T00:05:10Z")

</div>

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