# Output 0 or 1 based on array

**URL:** <https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923>\
**Category:** General\
**Created:** [7 February 2022 14:42 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923 "2022-02-07T14:42:35Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 21:58 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/23 "2022-02-07T21:58:34Z")

</div>

I am not sure - what you mean. I thought it is a buffer. If it is a numeric array [0,0] or [1,1] it is even simpler as the switch does check if first and second element of the array is equal- I see no difference.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/6/86338a4ff896b36a48a0dda4bcadd00f351a14f9.png)

so a next switch must only differentiate between 0 and 1 on one bit - if both are equal the second one is the same.  
If I use the false exit then I check also only one bit or field and know that the other one is the opposite.

---

<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:** [7 February 2022 22:12 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/24 "2022-02-07T22:12:17Z")

</div>

> [@mickym2](#):
>
> I thought it is a buffer

That is why I asked to see the actual data right at the start, as the question was ambiguous. You can see from the debug that it is a numeric array not a Buffer.

It can certainly be done using core nodes, but I think that sometimes a few lines in a function node is a reasonable alternative to a collection of core nodes.

What do you think is the best core node solution for the problem given the posted data?

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 22:36 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/25 "2022-02-07T22:36:34Z")

</div>

If this is a normal array - I would just use one switch node:

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

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/ae2c3a43a9f66d7b04afc089705520a12bb77dad.png)

---

<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:** [7 February 2022 22:40 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/26 "2022-02-07T22:40:38Z")

</div>

> [@Colin](#):
>
> You can see from the debug that it is a numeric array not a Buffer.

Strange as i see an array of objects in the data. So bits[0] and [1] would both be objects and should not be equal. But i do see it does not say object so i presume this is a debug array/buffer quirk.  
[edit] So I just learnt that javascript allows Non numeric arrays ( Associative Arrays).

@mickym2 May be better to join the array's first two elements in the switch property with a JSONata expression, then do string comparisons on the outputs

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [7 February 2022 22:50 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/27 "2022-02-07T22:50:11Z")

</div>

Ok you mean something like this - many ways to Rome. 😉

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/7/47898398e2f52a728c5c6b715f971f26601e8559.png)

---

<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:** [8 February 2022 07:16 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/28 "2022-02-08T07:16:52Z")

</div>

The value is not in msg.payload

---

<div class="post-metadata">

**Author:** ![mickym2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mickym2/32/36128_2.png) [@mickym2](https://discourse.nodered.org/u/mickym2)\
**Post date:** [8 February 2022 13:53 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/29 "2022-02-08T13:53:37Z")

</div>

I saw the original debug window - and saw that this was the output of the buffer node. Therefore I send some pictures how to change the buffer node -  
If you use my picture in posting 13 then you can directly use numbers 1-3 -masking with the value of 3.

But anyway - is no problem . The thread creator has posted your code in #18 as solution and therefore our discussion is obsolete as your proposal seems to be the preferred solution.

---

<div class="post-metadata">

**Author:** ![aallen](https://avatars.discourse-cdn.com/v4/letter/a/65b543/32.png) [@aallen](https://discourse.nodered.org/u/aallen)\
**Post date:** [8 February 2022 14:01 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/30 "2022-02-08T14:01:51Z")

</div>

Your solution is valid and I took it into account, but for simplicity i feel the function block is better for me. However, I definitely learned about the switch/buffer node from your reply, so thank you.

---

<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:** [8 February 2022 14:58 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/31 "2022-02-08T14:58:08Z")

</div>

> [@mickym2](#):
>
> I send some pictures how to change the buffer node

I had not realised that there was already a buffer node involved. Your suggestion makes sense to me now. 🙂

---

<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:** [22 February 2022 14:59 UTC](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923/32 "2022-02-22T14:59:00Z")

</div>

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

[Previous page](https://discourse.nodered.org/t/output-0-or-1-based-on-array/57923.md?page=1)
