# Node red if function for data selection

**URL:** <https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688>\
**Category:** General\
**Tags:** node-red-dashboard, function-node\
**Created:** [20 February 2024 02:34 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688 "2024-02-20T02:34:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitchysho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mitchysho/32/88452_2.png) [@mitchysho](https://discourse.nodered.org/u/mitchysho)\
**Post date:** [20 February 2024 02:34 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688/1 "2024-02-20T02:34:36Z")

</div>

Hi,

This is my complete message from my gateway  
2/20/2024, 10:29:13 AM[node: all](http://localhost:1880/#)kbeacon/publish/282C0221FD0B : msg.payload : Object

object  
msg: "advData"  
loc: object  
gmac: "282C0221FD0B"  
obj: array[4]  
0: object  
1: object  
2: object  
type: 1  
dmac: "BC5729028155"  
time: "2024-02-20 10:29:11"  
rssi: -73  
ver: 0  
vbatt: 3018  
temp: 29.8  
humidty: 79.49  
x0: 76  
y0: -30  
z0: 1041  
3: object

As you can see in the array, there are 4 objects, and at times will be more detected. I need to only display the data for object with specific dmac only. Then display into the dashboard. currently the object is randomized so i get data from different sensor for the object number.  
for example, in the above, the object 2 dmac is BC5729028155, but in the next data in this would change. so my dashboard will be a randomized data from sensors/object that the gateway picked up.

thank you. i am very2 new to this.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [20 February 2024 03:55 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688/2 "2024-02-20T03:55:25Z")

</div>

Hello @mitchysho,

Please use a debug node set to display msg.payload.  
At the right hand side of the debug output is a "Copy value" button.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/4/244a8e22d409dcef41d2f22bb42fd085b2d0c987.png)

Then back in the forum, on a new line click the `</>` button and paste the debug output over the top of  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/4/042077fec65e2e7ec0b5a4fa43cdd271efc8c35b.png)

Wrapping between the lines with three back ticks makes it _much_ easier to import and understand your data.

But I am pretty sure you can achieve what you want with

- A change node to move just the array to msg.payload
- A split node to make each array element it's own discrete message and
- A switch node to only pass the one where msg.payload.dmac is the value you are looking for.

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [20 February 2024 05:50 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688/3 "2024-02-20T05:50:24Z")

</div>

You could use a change node with an expression, something like:

```auto
**['BC5729028155' in dmac].temp

```

Which will return 29.8

This will filter based on the dmac provided.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/9/79c824730a65f359ee3931be8ca4b6926a35b90f.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:** [20 February 2024 07:49 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688/4 "2024-02-20T07:49:58Z")

</div>

Just to point out that `**['BC5729028155' in dmac].temp` is not the most efficient way to fine the specific dmacs, as using wild card search will have to look every where first.

I think `$$.payload.obj[$.dmac = "BC5729028155"]` would be better/more efficient. You can still add `.temp` if you want specific values to.  
Assuming I am reading the payload object correctly, as jbudd has pointed out there is a better way to copy and paste an object.

[edit] As to in a function as you asked in title, you would use the filter method [Array.prototype.filter() - JavaScript | MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)

```auto
msg.payload = msg.payload.obj.filter((obj) => obj.dmac === "BC5729028155");
retrun msg;

```

---

<div class="post-metadata">

**Author:** ![mitchysho](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/mitchysho/32/88452_2.png) [@mitchysho](https://discourse.nodered.org/u/mitchysho)\
**Post date:** [20 February 2024 12:45 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688/5 "2024-02-20T12:45:05Z")

</div>

> [@E1cid](#):
>
> $$.payload.obj[$.dmac = "BC5729028155"]

thank you. this seems to be working. all of you are wizard 🤯

---

<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 March 2024 12:45 UTC](https://discourse.nodered.org/t/node-red-if-function-for-data-selection/85688/6 "2024-03-05T12:45:21Z")

</div>

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