# How to extract some properties from an array of object?

**URL:** <https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488>\
**Category:** General\
**Tags:** function-node\
**Created:** [29 March 2022 11:22 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488 "2022-03-29T11:22:39Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 11:22 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/1 "2022-03-29T11:22:40Z")

</div>

I believe that's a pretty basic question but I can't sort it out :

Given the following array :

```auto
[
   {
       "TimeStamp":"2022-03-29T10:15:00.000Z",
       "Name":"SN: 2001680707",
       "Type":"SB 3300",
       "Serial":2001680707,
       "TotalYield":228433,
       "Power":600,
       "PVOutput":1
   },
   {
       "TimeStamp":"2022-03-29T10:10:00.000Z",
       "Name":"SN: 2001680707",
       "Type":"SB 3300",
       "Serial":2001680707,
       "TotalYield":228383,
       "Power":636,
       "PVOutput":1
   }
]

```

I would like to create an array of object from it containing only the TimeStamp and Power keys/values :

```auto
[
    {
         "TimeStamp" : "2022-03-29T10:15:00.000Z",
         "Power" : 600
    },
    {
       "TimeStamp" : "2022-03-29T10:10:00.000Z", 
        "Power" : 636
    }
]

```

I particularly love Sonata which I find very elegant, but a function would suit as well 😊

Thanks for your help,

Matthieu

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 11:58 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/2 "2022-03-29T11:58:42Z")

</div>

Have a look at this:

```auto
[{"id":"7dd0136182e54540","type":"inject","z":"753fdb2386ae42a9","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{ \"TimeStamp\":\"2022-03-29T10:15:00.000Z\", \"Name\":\"SN: 2001680707\", \"Type\":\"SB 3300\", \"Serial\":2001680707, \"TotalYield\":228433, \"Power\":600, \"PVOutput\":1 }, { \"TimeStamp\":\"2022-03-29T10:10:00.000Z\", \"Name\":\"SN: 2001680707\", \"Type\":\"SB 3300\", \"Serial\":2001680707, \"TotalYield\":228383, \"Power\":636, \"PVOutput\":1 }]","payloadType":"json","x":150,"y":120,"wires":[["c474eaf52ea22f1e"]]},{"id":"68f62b358958c059","type":"debug","z":"753fdb2386ae42a9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":120,"wires":[]},{"id":"c474eaf52ea22f1e","type":"change","z":"753fdb2386ae42a9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"[{\t \"TimeStamp\": payload[0].TimeStamp,\t \"Power\": payload[0].Power\t \t },\t {\t \"TimeStamp\": payload[1].TimeStamp,\t \"Power\": payload[1].Power\t }\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":400,"y":120,"wires":[["68f62b358958c059"]]}]

```

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 12:02 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/3 "2022-03-29T12:02:02Z")

</div>

This page explains how to get the path to object properties:  
[https://nodered.org/docs/user-guide/messages](https://nodered.org/docs/user-guide/messages)

Like this:  
 ![Screenshot-7](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5aa3bce5a56c674b71c74958d98abe973a3111b.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:** [29 March 2022 12:46 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/4 "2022-03-29T12:46:27Z")

</div>

Or, if the array is of variable length, then assuming it is in msg.payload a Function node containing this should do the job

```auto
msg.payload = msg.payload.map((element) => ({TimeStamp: element.TimeStamp, Power: element.Power}))
return msg;

```

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 13:28 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/5 "2022-03-29T13:28:51Z")

</div>

Thanks @ghayne, as @Colin guessed, my array's length is variable and therefore the obvious solution doesn't work, sorry for not mentioning it.

@Colin perfect ! I'm not fluent in Javascript. I did found the .map object but missed the `element` thing you used here. I'm good now, let's put some more nodes 😊

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 14:30 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/6 "2022-03-29T14:30:40Z")

</div>

@Colllin, I should actually have an array of arrays containing the objects.

Something like this :

```auto
[
    [
       {
         "TimeStamp" : "2022-03-29T10:15:00.000Z",
         "Power" : 600
       }
    ],
    [
       {
       "TimeStamp" : "2022-03-29T10:10:00.000Z", 
        "Power" : 636
      }
   ]
]

```

How would I achieve this ?

Txs

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 15:07 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/7 "2022-03-29T15:07:03Z")

</div>

Modify @colin's code by inserting array brackets:

```auto
msg.payload = msg.payload.map((element) => ([[{TimeStamp: element.TimeStamp, Power: element.Power}]]))
return msg;

```

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 15:12 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/8 "2022-03-29T15:12:36Z")

</div>

I really must learn Javascript. Thanks @ghayne !

---

<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:** [29 March 2022 15:14 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/9 "2022-03-29T15:14:54Z")

</div>

Does that do what you want? I would have thought you only want one set of brackets.  
`msg.payload = msg.payload.map((element) => ([{TimeStamp: element.TimeStamp, Power: element.Power}]))`

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 15:16 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/10 "2022-03-29T15:16:22Z")

</div>

Exactly @Colin. I was wondering why two brackets and soon discovered that one was more than enough 😉

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 15:16 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/11 "2022-03-29T15:16:50Z")

</div>

@colin, correct!

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 15:21 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/12 "2022-03-29T15:21:21Z")

</div>

So, just for fun, any idea how to do this using Jsonata ? There is a $map() function but no idea how to use it.

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 15:27 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/14 "2022-03-29T15:27:42Z")

</div>

```auto
[{"id":"7dd0136182e54540","type":"inject","z":"753fdb2386ae42a9","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{ \"TimeStamp\":\"2022-03-29T10:15:00.000Z\", \"Name\":\"SN: 2001680707\", \"Type\":\"SB 3300\", \"Serial\":2001680707, \"TotalYield\":228433, \"Power\":600, \"PVOutput\":1 }, { \"TimeStamp\":\"2022-03-29T10:10:00.000Z\", \"Name\":\"SN: 2001680707\", \"Type\":\"SB 3300\", \"Serial\":2001680707, \"TotalYield\":228383, \"Power\":636, \"PVOutput\":1 }]","payloadType":"json","x":150,"y":120,"wires":[["c474eaf52ea22f1e"]]},{"id":"68f62b358958c059","type":"debug","z":"753fdb2386ae42a9","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":120,"wires":[]},{"id":"c474eaf52ea22f1e","type":"change","z":"753fdb2386ae42a9","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$map(\t payload,\t function($v,$i) {\t [{\t \"TimeStamp\":$v.TimeStamp,\t \"Power\":$v.Power\t }] \t }\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":120,"wires":[["68f62b358958c059"]]}]

```

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 15:30 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/15 "2022-03-29T15:30:17Z")

</div>

Look here for an explanation:

> **[Higher order functions · JSONata](https://docs.jsonata.org/higher-order-functions)**
>
> \## \`$map()\`

---

<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:** [29 March 2022 15:43 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/16 "2022-03-29T15:43:34Z")

</div>

If you don't know javascript of jsonata I suggest starting with javascript. Once you are proficient with that then try jsonata. I unless you have more brain power than I have anyway. For me learning one new language at a time is more than enough 🙂

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 15:49 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/17 "2022-03-29T15:49:21Z")

</div>

True. However, my brain feels more adapted to the JSONata way of doing this, probably because it's more visual (and that's also why I love coding in NodeRed). @ghayne 's solution works brilliantly by the way :

```auto
$map(
   payload,
   function($v,$i) {
       [{
           "time":$toMillis($v.TimeStamp),
           "Power":$v.Power
       }] 
   }
)

```

Since I asked preferably the JSONata code, I think he deserves the "solution" tag, but thank you both of you really for your support.

Matthieu

---

<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:** [29 March 2022 15:55 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/18 "2022-03-29T15:55:32Z")

</div>

> [@pagaille](#):
>
> my brain feels more adapted to the JSONata way of doing this

In that case by all means do that. Mine most definitely is happier with the procedural route.

---

<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:** [29 March 2022 16:25 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/19 "2022-03-29T16:25:53Z")

</div>

No need to call the $map() function  
This also maps

```auto
$$.payload.[{
       "TimeStamp":$toMillis($.TimeStamp),
       "Power":$.Power
}] 

```

> **[Path Operators · JSONata](https://docs.jsonata.org/path-operators#-map)**
>
> The path operators underpin the declarative nature of the map/filter/reduce processing model in JSONata.

---

<div class="post-metadata">

**Author:** ![pagaille](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/pagaille/32/18808_2.png) [@pagaille](https://discourse.nodered.org/u/pagaille)\
**Post date:** [29 March 2022 16:51 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/20 "2022-03-29T16:51:52Z")

</div>

I love jsonata so much ❤❤❤

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [29 March 2022 17:20 UTC](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488/21 "2022-03-29T17:20:34Z")

</div>

Ah @E1cid I knew you were around somewhere 🙂

[Next page](https://discourse.nodered.org/t/how-to-extract-some-properties-from-an-array-of-object/60488.md?page=2)
