# Help with using objects in lists to display in a \`text\` node

**URL:** https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045
**Category:** General
**Created:** [2 August 2020 09:31 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045 "2020-08-02T09:31:43Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 09:31 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/1 "2020-08-02T09:31:43Z")

</div>

I'm programming up for my new project. (Parts still to arrive)

I am making a project with a 20 x 4 LCD display and buttons for input.

I'm not sure what type of buttons or how many. Which doesn't help I know.

What I am wanting to do is get the mechanics of it working for now.

One sticking point is _menus_, or maybe better: _lists_.

I'm also stuck how to get the _object_ built, but I'll get to that soon.

So, going a bit macro, I have a list of options which can be scrolled with an _up/down_ button and selected with a _select_. An **exit** option in case I go into a selection by mistake, maybe. That's another sticky point.

Going on from there, I am stuck how to do what I just said.  
I have played with thing like this, but that was more for a display and it used a node which created a list from the inputs. So that is probably a bit too high level for what I want/need.

I'll start with just 3 buttons for now: up; down; select.  
_up_ and _down_ move which part of the _object_ is sent to the `text` node.  
That is to get that part working. Ultimately it will go to the LCD. But at this level, it doesn't matter.  
_select_ will send a message with the displayed message ...... to a `debug` node when pressed.

Yes: I know this is beyond my skill set. But I won't learn if I don't try. So I don't want to be spoon fed the answer. Helped would be more appreciated.

(And this virus is getting serious here again. So I want to keep busy doing stuff I like.)

This is an example of what the source _object_ is:

```auto
["option 1","option 2","option 3","option 4"]

```

Just to be obvious. 😉

So I press a button (up/down) and it _scrolls_ through the options.  
It may be nice if it wrapped when it gets to either end.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 09:51 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/2 "2020-08-02T09:51:26Z")

</div>

This is a flow which kinda works.

```auto
[{"id":"49547ff4.9d7028","type":"function","z":"6ccc3cee.3fcb2c","name":"","func":"//msg = {\n// menu1:\n// [\"Show clock@2,0\",\"Show/edit alarms@2,1\",\"Edit time@2,2\",\"Something else@2,3\"],\n\n\n\n\nmsg = {\n options:\n [\"option 1\",\"option 2\",\"option 3\",\"option 4\"]\n}\n\nflow.set(\"options\",msg.options);\nreturn msg;","outputs":1,"noerr":0,"x":430,"y":1960,"wires":[["4a8a915a.21af18"]]},{"id":"35476e0e.c06e72","type":"inject","z":"6ccc3cee.3fcb2c","name":"","topic":"","payload":"press","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":230,"y":1960,"wires":[["49547ff4.9d7028"]]},{"id":"4a8a915a.21af18","type":"debug","z":"6ccc3cee.3fcb2c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":580,"y":1960,"wires":[]},{"id":"5512487e.fc3698","type":"function","z":"6ccc3cee.3fcb2c","name":"","func":"var p = context.get(\"pointer\") || 0;\n\nif (msg.payload == \"up\")\n{\n // up\n p = p + 1;\n context.set(\"pointer\",p);\n}\nif (msg.payload == \"down\")\n{\n // down\n p = p - 1;\n context.set(\"pointer\",p);\n}\n\nvar t = flow.get(\"options\");\n//node.warn(\"T is set to \" + t);\n\nmsg.payload = t[p];\n\n\n\nreturn msg;","outputs":1,"noerr":0,"x":470,"y":2090,"wires":[["9a55827c.a9b6e","5010ea80.105aec"]]},{"id":"5010ea80.105aec","type":"debug","z":"6ccc3cee.3fcb2c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","x":650,"y":2130,"wires":[]},{"id":"f334bfcb.c29ce8","type":"inject","z":"6ccc3cee.3fcb2c","name":"","topic":"","payload":"up","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":2030,"wires":[["5512487e.fc3698"]]},{"id":"d0da956a.05ed18","type":"inject","z":"6ccc3cee.3fcb2c","name":"","topic":"","payload":"down","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":2160,"wires":[["5512487e.fc3698"]]},{"id":"76321458.07a88c","type":"inject","z":"6ccc3cee.3fcb2c","name":"","topic":"","payload":"start","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":200,"y":2090,"wires":[["5512487e.fc3698"]]}]

```

It doesn't have wrapping.

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [2 August 2020 09:52 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/3 "2020-08-02T09:52:55Z")

</div>

OK - i went through this in the old days with Arduino's and LCDs

As a high level description

1. Build an array of Lines to show in the menu - one line per option
2. Build an array that reflects each line your display (in your case) 0-3 (or 1-4)

Label a button as Up and one as down

When you press a button you cycle through the array that reflects your lines on the screen and then offset the lines that are currently displayed by 1 (or -1)

You then enter your display routine and put the appropriate starting array element onto the screen starting from the top

So as an example you start off with

DL = Display Line  
ML = Menu Line

DL(1) = ML(1)  
DL(2) = ML(2)  
DL(3) = ML(3)  
DL(4) = ML(4)

(or you might want to Keep Line 4 as a Menu control line etc)

You press the up button and

DL(1) = ML(2)  
DL(2) = ML(3)  
DL(3) = ML(4)  
DL(4) = ML(5)

Press it again and

DL(1) = ML(3)  
DL(2) = ML(4)  
DL(3) = ML(5)  
DL(4) = ML(6)

(You can handle menu wrapping logic etc that way quite easily as well)

Craig

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 09:58 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/4 "2020-08-02T09:58:57Z")

</div>

Thanks Craig.

But I am doing all this in Node-Red, rather than Arduino. My head is not enjoying it as much as I would like. 😉

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [2 August 2020 09:59 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/5 "2020-08-02T09:59:43Z")

</div>

Yep - sorry i was just showing you the high level logic - i no longer use Arduino and LCDs etc

Craig

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 10:01 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/6 "2020-08-02T10:01:10Z")

</div>

I just edited the `function` node to this:

```auto
var p = context.get("pointer") || 0;

if (msg.payload == "up")
{
    // up
    p = (p + 1) % 3;
    context.set("pointer",p);
}
if (msg.payload == "down")
{
    // down
    p = (p + 3 - 1) % 3;
    context.set("pointer",p);
}

var t = flow.get("options");
//node.warn("T is set to " + t);

msg.payload = t[p];

return msg;

```

And it wraps.

_Problem_:  
Variable _object_ lengths.

So what I could do is stick the length at the end.  
The first is possible, but please indulge me.

How do I get the last element in an array?  
I am sure I have seen it somewhere.

I will go and search, but while I am here.... Though I would ask.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 10:02 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/7 "2020-08-02T10:02:28Z")

</div>

Oh, that's a shame.

I would nearly do this project with a teensy, but as an excuse to keep my hand in Node-Red I am doing the back end with Node-Red.

I do like my Arduino alarm clock though. It is still going good.

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 10:08 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/8 "2020-08-02T10:08:23Z")

</div>

This is another bit of code in the `function` node:

```auto
var t = flow.get("options");
var l = (t.length) - 2;

//node.warn(l);
//node.warn("T is set to " + t);

var p = context.get("pointer") || 0;

if (msg.payload == "up")
{
    // up
    p = (p + 1) % l;
    context.set("pointer",p);
}
if (msg.payload == "down")
{
    // down
    p = (p + 3 - 1) % l;
    context.set("pointer",p);
}

msg.payload = t[p];

return msg;

```

---

<div class="post-metadata">

### Author: ![craigcurtin](https://avatars.discourse-cdn.com/v4/letter/c/94ad74/32.png) [@craigcurtin](https://discourse.nodered.org/u/craigcurtin)
#### Post date: [2 August 2020 10:09 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/9 "2020-08-02T10:09:04Z")

</div>

Like i said - use array objects (each of which is a string)

You know your max line length is 20 so you do not need to worry about lines longer than this (as you will not store them)

You do not reference the individual objects (characters) in each line - you just retrieve and display the full lines

Craig

---

<div class="post-metadata">

### Author: ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)
#### Post date: [2 August 2020 10:11 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/10 "2020-08-02T10:11:29Z")

</div>

So would you mind having a _peep_ at the code I posted and import the latest code for the `function` node.

I believe that is working.

So that being a working thing: before I _call_ (use) this routine, I would copy the _object_ into the `flow.options` _object_.

---

<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: [1 October 2020 10:11 UTC](https://discourse.nodered.org/t/help-with-using-objects-in-lists-to-display-in-a-text-node/31045/11 "2020-10-01T10:11:41Z")

</div>

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