# If(.. && ...) Please help with this bit of code

**URL:** https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278
**Category:** General
**Created:** [20 September 2021 09:20 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278 "2021-09-20T09:20:37Z")
**Posts on this page:** 15
**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: [20 September 2021 09:20 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/1 "2021-09-20T09:20:37Z")

</div>

I have never been good at handling this structure.

This bit of code _should_ (maybe even did used to) work.

But now it is failing and I am getting `22` for `x`.

```auto
if(Array.isArray(msg.table) && msg.table.length)
{
    x = msg.table[m];
} else
{
    x = 22; // Default if not set.
}

```

The payload going in looks like this:

```auto
{"_msgid":"452211cf.9ee4c","payload":1632129281747,"topic":"","table":{"1":20,"2":24,"3":28,"4":32,"5":36,"6":40,"7":40,"8":36,"9":32,"10":28,"11":24,"12":20},"month":"09","settings":{"input":"2021-09-20T09:14:41.747Z","input_format":"","input_tz":"Australia/Sydney","output_format":"MM","output_locale":"en_AU","output_tz":"Australia/Sydney"}}

```

Where have I messed up?  
(or better:  
What code do I need so it gives me the desired `32` instead of the _default_ `22`?)

---

<div class="post-metadata">

### Author: ![hardillb](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/hardillb/32/12373_2.png) [@hardillb](https://discourse.nodered.org/u/hardillb)
#### Post date: [20 September 2021 09:28 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/2 "2021-09-20T09:28:58Z")

</div>

`msg.table` is an object, not an array in the example you have given.

---

<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: [20 September 2021 09:30 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/3 "2021-09-20T09:30:18Z")

</div>

Ok, so the second _test_ in the `if` will fail.

(Idiot hat on)

What do I need to get it to work?

(Though I see SteveMcl is replying)

---

<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: [20 September 2021 09:30 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/4 "2021-09-20T09:30:37Z")

</div>

> [@Trying\_to\_learn](#):
>
> `,"table":{"1":20,"2":24,"3":28,"4":32,"5":36,"6":40,"7":40,"8":36,"9":32,"10":28,"11":24,"12":20}`

msg.table is not an array. It's an object.

> [@Trying\_to\_learn](#):
>
> `if(Array.isArray(msg.table) && msg.table.length)`

Try `if(msg.table && Object.keys(msg.table).length)`

---

<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: [20 September 2021 09:33 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/5 "2021-09-20T09:33:01Z")

</div>

Thanks Steve.  
Appreciated.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 September 2021 09:34 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/6 "2021-09-20T09:34:40Z")

</div>

If you have control of the data then change it so that it _is_ an array.

---

<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: [20 September 2021 09:40 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/7 "2021-09-20T09:40:01Z")

</div>

I kind of do and don't.

I have a `flow.table` which is the same structure.... an object.  
I _lookup_ the value from the month as shown in the code from the code I posted above.

I was going to access the `flow` level, but put that into the message before the node and parsed it as part of the incoming message.

What would be the _advantage_ of making it an array rather than an object?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 September 2021 10:24 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/8 "2021-09-20T10:24:51Z")

</div>

> [@Trying\_to\_learn](#):
>
> What would be the _advantage_ of making it an array rather than an object?

Efficency, plus simpler to access. The fact that you intuitively thought it was an array is a giveaway.

---

<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: [20 September 2021 10:41 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/9 "2021-09-20T10:41:39Z")

</div>

Would  
`x = (msg.table[m] || 22);`  
work the same?

---

<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: [20 September 2021 20:31 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/10 "2021-09-20T20:31:09Z")

</div>

I'm not sure that is the correct use of the term `intuitively`. 😉

I'll go off and see if I can work out how to use `arrays`.

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [20 September 2021 20:47 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/11 "2021-09-20T20:47:27Z")

</div>

> [@Trying\_to\_learn](#):
>
> intuitive

Using or based on what one feels to be true even without conscious reasoning.

So if you didn't use conscious reasoning to determine that it was an array, but just felt that it was, then that was intuitive.

---

<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: [20 September 2021 20:49 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/12 "2021-09-20T20:49:51Z")

</div>

Maybe. But stupidity is a good contender as well.

I have a lot of incorrect associations between things and use them wrongly often.

Anyway, I'll keep looking at arrays.

Lines like this don't help me:  
`Arrays are a special type of objects`  
(Just mentioning)

---

<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: [20 September 2021 20:59 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/13 "2021-09-20T20:59:13Z")

</div>

So the _advantage_ I can discern is that rather than needing two _entries_ per line (as mine has the month value and then the value I want) I would only need one _entry_. That being the value.

Am I correct?

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [21 September 2021 07:04 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/14 "2021-09-21T07:04:37Z")

</div>

Plus the vast set of Array functions that you get with arrays to allow handling them. So they are generally much easier to deal with (for data that lends itself to being in an array).

---

<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: [5 October 2021 07:05 UTC](https://discourse.nodered.org/t/if-please-help-with-this-bit-of-code/51278/15 "2021-10-05T07:05:29Z")

</div>

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