# Converting/ Rebuilding Array of Objects {different length}

**URL:** <https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190>\
**Category:** General\
**Created:** [25 March 2021 10:55 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190 "2021-03-25T10:55:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eintracht00](https://avatars.discourse-cdn.com/v4/letter/e/3d9bf3/32.png) [@Eintracht00](https://discourse.nodered.org/u/Eintracht00)\
**Post date:** [25 March 2021 10:55 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/1 "2021-03-25T10:55:50Z")

</div>

Hey, I get data from MSSQL with this code where the payload is the all entries of the named "key"  
This is the SELECT-Code:

 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/f/ef28849a20f0a09dcc1777cdfc87730b98107a35.png)  
The payload I get is an array with all entries of named "key" I selected:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/4/c4cfdeb3be7b84dbea88803b937b4fd60d73f131.png)  
Now i want to rebuild this payload. {not every payload array got same count of entries}  
The new payload should be like the following picture (1 key called "key" with the value of it, and 1 key as array called "Signalrauschen (dB)" with all their values of the full payload and finally 1 key as array called "Signalstärke (dBm)" with all their values of the full payload. Finally Payload should look like this:  
 ![grafik](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/7/b73b53e5842c71bae30d34b4b51ea1dd06ecda36.png)

So I need help to get a function all values from all date.  
and once a function to only get the values from now Date().

---

<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:** [25 March 2021 11:34 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/2 "2021-03-25T11:34:51Z")

</div>

A function node would work. Pass the msg to a function and just scan it pushing the values where they are needed.

```auto

const array1 = [
  {"key": "123", "Signalstärke (dBm)":-109, "Signalrauschen (dB)": 1}, 
  {"key": "123", "Signalstärke (dBm)":-106, "Signalrauschen (dB)": 3.8}, 
  {"key": "123", "Signalstärke (dBm)":-96, "Signalrauschen (dB)": 2.8}, 
  {"key": "123", "Signalstärke (dBm)":-92, "Signalrauschen (dB)": 4.2}
]; //demo data - replace this with const array1 = msg.payload;

var reduced = {key: "", "Signalstärke (dBm)": [], "Signalrauschen (dB)": []};
for(let i = 0; i < array1.length; i++) {
  const el = array1[i];
  reduced.key = el.key;
  reduced["Signalrauschen (dB)"].push(el["Signalrauschen (dB)"]);
  reduced["Signalstärke (dBm)"].push(el["Signalstärke (dBm)"]);
}

msg.payload = reduced;
return msg;

```

---

<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:** [25 March 2021 11:54 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/3 "2021-03-25T11:54:33Z")

</div>

you can also do this in a change node using the following JSONata expression

```auto
payload.ProgramNumber.value.${
   "Signalstärke (dBm)": `Signalstärke (dBm)`,
   "Signalrauschen (dB)": `Signalrauschen (dB)`
}

```

```auto
[{"id":"84127a3a.646268","type":"inject","z":"5a245aa1.510164","name":"","props":[{"p":"payload.ProgramNumber.value","v":"[{\"key\":\"123\",\"Signalstärke (dBm)\":-109,\"Signalrauschen (dB)\":1},{\"key\":\"123\",\"Signalstärke (dBm)\":-106,\"Signalrauschen (dB)\":3.8},{\"key\":\"123\",\"Signalstärke (dBm)\":-96,\"Signalrauschen (dB)\":2.8},{\"key\":\"123\",\"Signalstärke (dBm)\":-92,\"Signalrauschen (dB)\":4.2}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":210,"y":3400,"wires":[["b2aa8e7f.b22818"]]},{"id":"b2aa8e7f.b22818","type":"change","z":"5a245aa1.510164","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.ProgramNumber.value.${\"Signalstärke (dBm)\": `Signalstärke (dBm)`,\"Signalrauschen (dB)\": `Signalrauschen (dB)`}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":430,"y":3420,"wires":[["c354c6a3.09a65"]]},{"id":"c354c6a3.09a65","type":"debug","z":"5a245aa1.510164","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":600,"y":3400,"wires":[]}]

```

 ![Screenshot_20210325-115405](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/719883259190e34cf07d5ad07a71c4c838b2a0c1.png)

---

<div class="post-metadata">

**Author:** ![Eintracht00](https://avatars.discourse-cdn.com/v4/letter/e/3d9bf3/32.png) [@Eintracht00](https://discourse.nodered.org/u/Eintracht00)\
**Post date:** [25 March 2021 11:54 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/4 "2021-03-25T11:54:55Z")

</div>

that works pretty well for all date.  
is there a way to only move the data into the new payload if the date is from today?

---

<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:** [25 March 2021 11:55 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/5 "2021-03-25T11:55:51Z")

</div>

Yeah, only query the database for today's data with a where clause.

---

<div class="post-metadata">

**Author:** ![Eintracht00](https://avatars.discourse-cdn.com/v4/letter/e/3d9bf3/32.png) [@Eintracht00](https://discourse.nodered.org/u/Eintracht00)\
**Post date:** [25 March 2021 11:57 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/6 "2021-03-25T11:57:57Z")

</div>

how should it work if my [seen] is like Date.Month.Year, Hours:Minutes:Seconds, and i only want entries from the Day without care about the time?

---

<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:** [25 March 2021 12:00 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/7 "2021-03-25T12:00:55Z")

</div>

Several options but the best bet is to store data as proper date time instead of string then in the where clause of your SQL check if datetime is dateadd -24h or use datepart to check dmy is equal to today.

This is simple SQL stuff. Do a web search.

---

<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:** [8 April 2021 12:00 UTC](https://discourse.nodered.org/t/converting-rebuilding-array-of-objects-different-length/43190/8 "2021-04-08T12:00:55Z")

</div>

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