# Search Array and return corresponding data?

**URL:** https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889
**Category:** General
**Created:** [7 October 2020 13:37 UTC](https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889 "2020-10-07T13:37:54Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ojsint](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@ojsint](https://discourse.nodered.org/u/ojsint)
#### Post date: [7 October 2020 13:37 UTC](https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889/1 "2020-10-07T13:37:54Z")

</div>

Hello, relative newbie to Node-Red, but having followed the various tutorials and searched what I can on here I have a few pages up and running.

My latest issue, that I am hoping the collective can assit with, is the code for looking up an id number in an array and returning various corresponding values.

The msg object/array I receive from an API request is as follows:

```auto
{
  "label": "MiHome",
  "data": [
    {
      "id": 260882,
      "name": "UPS",
      "on": 1,
      "power": 116
    },
    {
      "id": 253105,
      "name": "PC",
      "on": 1,
      "power": null
    },
    {
      "id": 253110,
      "name": "TV",
      "on": 0,
      "power": null
    },
    {
      "id": 253593,
      "name": "Desk",
      "on": 1,
      "power": null
    },
    {
      "id": 257639,
      "name": "Printer",
      "on": 0,
      "power": 0
    },
    {
      "id": 260431,
      "name": "Fan",
      "on": 0,
      "power": null
    },
    {
      "id": 260448,
      "name": "Socket",
      "on": 0,
      "power": 0
    },
  ]
}

```

I want to then be able to lookup the ID number and return the on/off state (0 or 1) and the power reading (where applicable). I've tried searching the forum, and lokoing at the JSONata pages but can't seem to get anything to work?

It would be great if someone can point me in the right direction of the way to go about this?

Many thanks  
Olly

---

<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 October 2020 13:53 UTC](https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889/2 "2020-10-07T13:53:53Z")

</div>

An example using change node and JSONata expression

```auto
[{"id":"187967a.a95e618","type":"inject","z":"4484ee28.caa3c","name":"","props":[{"p":"payload"},{"p":"search","v":" 253105","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"label\":\"MiHome\",\"data\":[{\"id\":260882,\"name\":\"UPS\",\"on\":1,\"power\":116},{\"id\":253105,\"name\":\"PC\",\"on\":1,\"power\":null},{\"id\":253110,\"name\":\"TV\",\"on\":0,\"power\":null},{\"id\":253593,\"name\":\"Desk\",\"on\":1,\"power\":null},{\"id\":257639,\"name\":\"Printer\",\"on\":0,\"power\":0},{\"id\":260431,\"name\":\"Fan\",\"on\":0,\"power\":null},{\"id\":260448,\"name\":\"Socket\",\"on\":0,\"power\":0}]}","payloadType":"json","x":130,"y":500,"wires":[["4119a2a6.a337dc"]]},{"id":"4119a2a6.a337dc","type":"change","z":"4484ee28.caa3c","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$filter(\t payload.data,\t function($v) {$v.id = search }\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":500,"wires":[["269a25ff.79887a"]]},{"id":"269a25ff.79887a","type":"debug","z":"4484ee28.caa3c","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":590,"y":360,"wires":[]}]

```

msg.payload holds the data.  
msg.search holds the search id number.

---

<div class="post-metadata">

### Author: ![ojsint](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@ojsint](https://discourse.nodered.org/u/ojsint)
#### Post date: [7 October 2020 13:55 UTC](https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889/3 "2020-10-07T13:55:23Z")

</div>

Thanks, I'll give that a go, much apprecaited

---

<div class="post-metadata">

### Author: ![ojsint](https://avatars.discourse-cdn.com/v4/letter/o/7ba0ec/32.png) [@ojsint](https://discourse.nodered.org/u/ojsint)
#### Post date: [7 October 2020 14:02 UTC](https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889/4 "2020-10-07T14:02:20Z")

</div>

That seems to be working perfectly, many thanks - much appareciated.  
I can then use funcitons to get the raw data out to plot/display.

Cheers

---

<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: [21 October 2020 14:02 UTC](https://discourse.nodered.org/t/search-array-and-return-corresponding-data/33889/5 "2020-10-21T14:02:59Z")

</div>

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