# Working with data in an array

**URL:** <https://discourse.nodered.org/t/working-with-data-in-an-array/53004>\
**Category:** General\
**Created:** [31 October 2021 09:59 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004 "2021-10-31T09:59:28Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [31 October 2021 09:59 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/1 "2021-10-31T09:59:29Z")

</div>

Hey guys and gals.

I'm currently scraping a website to get football stats. I've been able to most of the formatting etc and move the data in to fields but I need a bit of help understanding how to finish it off.

Currently I have this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/1/611631269a06aaa19cbfe0259eebed92a41a327f.png)

I'd like to split msg.payload[1] up a bit more, I'd like the first number, date, time and team names to be in different parts of the same array.

Also, something else that I'm not able to understand how to do is split msg.payload[2] up. I'd like the number splitting up every two digits in to different parts of the same array.

---

<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:** [31 October 2021 10:15 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/2 "2021-10-31T10:15:33Z")

</div>

> [@Romprod](#):
>
> I'd like the first number, date, time and team names to be in different parts of the same array.

To clarify can you show exactly what you would like to get out, given the input data you have shown.

---

<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:** [31 October 2021 10:37 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/3 "2021-10-31T10:37:56Z")

</div>

As you havent specified exactly what you want to do with the data - i did something I might want...

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/816ff0cfd0d21377dd40aca928a4d094c37a5bd6.png)

Demo flow (CTRL+I to import)

```auto
[{"id":"538c015aa81cdd5b","type":"inject","z":"af952aeaa20f4f97","name":"Norwich-Leads","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\" ENGLAND - PREMIER LEAGUE\",\"12 2021-10-31 14:00-Norwich-Leads\",\"11111111111111111111111111\"]","payloadType":"json","x":2620,"y":140,"wires":[["242e92f4e3188b31"]]},{"id":"29e4ad304101b4a5","type":"inject","z":"af952aeaa20f4f97","name":"Villa-WHam","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[\" ENGLAND - PREMIER LEAGUE\",\"X2 2021-10-31 16:30-Aston Villa-West Ham\",\"22222222222222222222222222\"]","payloadType":"json","x":2610,"y":180,"wires":[["242e92f4e3188b31"]]},{"id":"242e92f4e3188b31","type":"function","z":"af952aeaa20f4f97","name":"nice-ification 😉","func":"let [countryLeague, data, id] = msg.payload;\nlet countryLeagueParts = countryLeague.split(\" - \");\nlet country = countryLeagueParts[0].trim();\nlet league = countryLeagueParts[1].trim();\nlet number = (data.slice(0,2));\nlet date = new Date(data.slice(3,19));\nlet teams = data.slice(20).split(\"-\");\n\nmsg.payload = {\n country,\n league,\n number,\n date,\n homeTeam: teams[0],\n awayTeam: teams[1],\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2800,"y":160,"wires":[["04848441442b51d5"]]},{"id":"04848441442b51d5","type":"debug","z":"af952aeaa20f4f97","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":2810,"y":220,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [31 October 2021 11:46 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/4 "2021-10-31T11:46:38Z")

</div>

That's almost perfect thankyou. Apologies for not giving an example of what I wanted to do with the data, but you got it correct anyways! 🙂

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/d/fdafa7fb6d5dfa206fec306f86e752ab5f515c3f.png)

One last thing that I'm trying to attempt is to split the last number up also into different objects. So for msg.payload[2] I'd like that splitting up every 2 numbers into separate objects.

For eg:

```auto
country: "England"
league: "Premier League"
number: "12"
date: "2021-10-31T14:00:00.000Z"
homeTeam: "Norwich"
awayTeam: "Leeds"
home: "42"
draw: "26"
away: "32"
HT1: "26"
HTX: "46"
HT2: "28"
o1.5: "89"
o2.5: "67"
o3.5: "38"
bts: "57"
ots: "43"

```

I hope that makes sense

---

<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:** [31 October 2021 12:11 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/5 "2021-10-31T12:11:47Z")

</div>

Here is similar using a change node and JSONata expression.You can converted string number to numbers also if you need to.

```auto
[{"id":"33d085d8.0d8352","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.(\t $bits0 := $split($[0], \"-\").$trim($);\t $bits1 := [\t $append(\t $split($[1], /-| /)[[0..4]],\t $split($[1], \"-\")[[3,4]]\t )\t ].$trim($);\t $bits2 := [$match($[2], /\\d\\d/)].match;\t {\t \"country\": $bits0[0],\t \"league\": $bits0[1],\t \"number\": $bits1[0],\t \"date\": $bits1[1] & \"-\" & $bits1[2] & \"-\" & $bits1[3],\t \"time\": $bits1[4],\t \"timestamp\": $bits1[1] & \"-\" & $bits1[2] & \"-\" & $bits1[3] &\"T\" & $bits1[4] & \":00.000Z\",\t \"homeTeam\": $bits1[5],\t \"awayTeam\": $bits1[6],\t \"home\": $bits2[0],\t \"draw\": $bits2[1],\t \"away\": $bits2[2],\t \"HT1\": $bits2[3],\t \"HTX\": $bits2[4],\t \"HT2\": $bits2[5],\t \"o1.5\": $bits2[6],\t \"o2.5\": $bits2[7],\t \"o3.5\": $bits2[8],\t \"bts\": $bits2[9],\t \"ots\": $bits2[10]\t }\t\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":3860,"wires":[["1c48acb2.d89fd3"]]},{"id":"f50918bd.89d3f8","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[\" england - premier league\",\"12 2021-10-31 14:00-norwic-leeds\",\"2435465768798975642858\"],[\" england - premier league\",\"12 2021-10-31 14:00-astoin villa-leeds\",\"2435465768798975642858\"],[\" england - premier league\",\"12 2021-10-31 14:00-brighton-leeds\",\"2435465768798975642858\"]]","payloadType":"json","x":160,"y":3840,"wires":[["33d085d8.0d8352"]]},{"id":"1c48acb2.d89fd3","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":630,"y":3880,"wires":[]}]

```

Input

```auto
[[" england - premier league","12 2021-10-31 14:00-norwic-leeds","2435465768798975642858"],[" england - premier league","12 2021-10-31 14:00-astoin villa-leeds","2435465768798975642858"],[" england - premier league","12 2021-10-31 14:00-brighton-leeds","2435465768798975642858"]]

```

Output

```auto
[{"country":"england","league":"premier league","number":"12","date":"2021-10-31","time":"14:00","timestamp":"2021-10-31T14:00:00.000Z","homeTeam":"norwic","awayTeam":"leeds","home":"24","draw":"35","away":"46","HT1":"57","HTX":"68","HT2":"79","o1.5":"89","o2.5":"75","o3.5":"64","bts":"28","ots":"58"},{"country":"england","league":"premier league","number":"12","date":"2021-10-31","time":"14:00","timestamp":"2021-10-31T14:00:00.000Z","homeTeam":"astoin villa","awayTeam":"leeds","home":"24","draw":"35","away":"46","HT1":"57","HTX":"68","HT2":"79","o1.5":"89","o2.5":"75","o3.5":"64","bts":"28","ots":"58"},{"country":"england","league":"premier league","number":"12","date":"2021-10-31","time":"14:00","timestamp":"2021-10-31T14:00:00.000Z","homeTeam":"brighton","awayTeam":"leeds","home":"24","draw":"35","away":"46","HT1":"57","HTX":"68","HT2":"79","o1.5":"89","o2.5":"75","o3.5":"64","bts":"28","ots":"58"}]

```

expression

```auto
$$.payload.(
   $bits0 := $split($[0], "-").$trim($);
   $bits1 := [
       $append(
           $split($[1], /-| /)[[0..4]],
           $split($[1], "-")[[3,4]]
       )
   ].$trim($);
   $bits2 := [$match($[2], /\d\d/)].match;
   {
       "country": $bits0[0],
       "league": $bits0[1],
       "number": $bits1[0],
       "date": $bits1[1] & "-" & $bits1[2] & "-" & $bits1[3],
       "time": $bits1[4],
       "timestamp": $bits1[1] & "-" & $bits1[2] & "-" & $bits1[3] &"T" & $bits1[4] & ":00.000Z",
       "homeTeam": $bits1[5],
       "awayTeam": $bits1[6],
       "home": $bits2[0],
       "draw": $bits2[1],
       "away": $bits2[2],
       "HT1": $bits2[3],
       "HTX": $bits2[4],
       "HT2": $bits2[5],
       "o1.5": $bits2[6],
       "o2.5": $bits2[7],
       "o3.5": $bits2[8],
       "bts": $bits2[9],
       "ots": $bits2[10]
       }

)

```

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [31 October 2021 20:10 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/6 "2021-10-31T20:10:07Z")

</div>

Thanks for the help.

Almost there with this option.

My messages are one game per message, so all the info is in one array per message rather than multiple arrays per message (at least I think that's how I need to explain it)

So my input would be slightly different

This is mine

```auto
[
    " england - premier league",
    "12 2021-10-31 14:00-norwic-leeds",
    "2435465768798975642858"
]

```

This is yours

```auto
[
    [
        " england - premier league",
        "12 2021-10-31 14:00-norwic-leeds",
        "2435465768798975642858"
    ]
]

```

So the diff is as below

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

Sorry, I have no idea how to change your code to what I need for my input vs yours.

---

<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:** [31 October 2021 20:23 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/7 "2021-10-31T20:23:57Z")

</div>

Sorry for error , but mistakes happen when copying from image. Best to paste text, so no mistakes and easier in long run.

```auto
(
   $bits0 := $split(payload[0], "-").$trim($);
   $bits1 := [
       $append(
           $split(payload[1], /(-|\s)+/)[[0..4]],
           $split(payload[1], "-")[[3,4]]
       )
   ].$trim($);
   $bits2 := [$match(payload[2], /\d\d/)].match;
   {
       "country": $bits0[0],
       "league": $bits0[1],
       "number": $bits1[0],
       "date": $bits1[1] & "-" & $bits1[2] & "-" & $bits1[3],
       "time": $bits1[4],
       "timestamp": $bits1[1] & "-" & $bits1[2] & "-" & $bits1[3] &"T" & $bits1[4] & ":00.000Z",
       "homeTeam": $bits1[5],
       "awayTeam": $bits1[6],
       "home": $bits2[0],
       "draw": $bits2[1],
       "away": $bits2[2],
       "HT1": $bits2[3],
       "HTX": $bits2[4],
       "HT2": $bits2[5],
       "o1.5": $bits2[6],
       "o2.5": $bits2[7],
       "o3.5": $bits2[8],
       "bts": $bits2[9],
       "ots": $bits2[10]
       }

)

```

Here is edited expression. You will see payload has moved to where $[0] $[1] and $[2] used to be.

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [31 October 2021 21:54 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/8 "2021-10-31T21:54:45Z")

</div>

Sorry I'm not following your example.

This is what I have so far

```auto
[{"id":"d88dd470.0ac7b8","type":"inject","z":"ffe44f33e5843155","name":"make request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":120,"y":40,"wires":[["874a3d4e.9b666"]]},{"id":"874a3d4e.9b666","type":"http request","z":"ffe44f33e5843155","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://www.statarea.com/predictions/date/2021-11-01/starttime","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":120,"y":90,"wires":[["90243cc1.87edc"]]},{"id":"90243cc1.87edc","type":"html","z":"ffe44f33e5843155","name":"EPL","property":"payload","outproperty":"payload","tag":"#\\30 10827","ret":"text","as":"multi","x":290,"y":40,"wires":[["db4a77d111714b53"]]},{"id":"db4a77d111714b53","type":"change","z":"ffe44f33e5843155","name":"Change","rules":[{"t":"change","p":"payload","pt":"msg","from":"your","fromt":"str","to":",your","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"prediction","fromt":"str","to":"prediction,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"LEAGUE","fromt":"str","to":"LEAGUE,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"TIP","fromt":"str","to":"TIP,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1X2H1HXH21.52.53.5BTSOTS","fromt":"str","to":",1X2H1HXH21.52.53.5BTSOTS,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"close","fromt":"str","to":",close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":90,"wires":[["b45b7cdf7ba8e354"]]},{"id":"b45b7cdf7ba8e354","type":"change","z":"ffe44f33e5843155","name":"Build array","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split(payload,',')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":460,"y":90,"wires":[["600f6ed3a3db3390"]]},{"id":"600f6ed3a3db3390","type":"change","z":"ffe44f33e5843155","name":"Change","rules":[{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[2]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"change","p":"payload[0]","pt":"msg","from":"ENGLAND","fromt":"str","to":"England","tot":"str"},{"t":"change","p":"payload[0]","pt":"msg","from":"PREMIER LEAGUE","fromt":"str","to":"Premier League","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":470,"y":40,"wires":[["242e92f4e3188b31","a36ea0445dfcfdab"]]},{"id":"242e92f4e3188b31","type":"function","z":"ffe44f33e5843155","name":"","func":"let [countryLeague, data, id] = msg.payload;\nlet countryLeagueParts = countryLeague.split(\" - \");\nlet country = countryLeagueParts[0].trim();\nlet league = countryLeagueParts[1].trim();\nlet number = (data.slice(0,2));\nlet date = new Date(data.slice(3,19));\nlet teams = data.slice(20).split(\"-\");\n\nmsg.payload = {\n country,\n league,\n number,\n date,\n homeTeam: teams[0],\n awayTeam: teams[1],\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":690,"y":40,"wires":[["04848441442b51d5"]]},{"id":"04848441442b51d5","type":"debug","z":"ffe44f33e5843155","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":940,"y":40,"wires":[]},{"id":"a36ea0445dfcfdab","type":"change","z":"ffe44f33e5843155","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t $bits0 := $split(payload[0], \"-\").$trim($);\t $bits1 := [\t $append(\t $split(payload[1], /-| /)[[0..4]],\t $split(payload[1], \"-\")[[3,4]]\t )\t ].$trim($);\t $bits2 := [$match(payload[2], /\\d\\d/)].match;\t {\t \"home\": $bits2[0],\t \"draw\": $bits2[1],\t \"away\": $bits2[2],\t \"HT1\": $bits2[3],\t \"HTX\": $bits2[4],\t \"HT2\": $bits2[5],\t \"o1.5\": $bits2[6],\t \"o2.5\": $bits2[7],\t \"o3.5\": $bits2[8],\t \"bts\": $bits2[9],\t \"ots\": $bits2[10]\t }\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":110,"wires":[["609933cde0b38546"]]},{"id":"609933cde0b38546","type":"debug","z":"ffe44f33e5843155","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":940,"y":110,"wires":[]}]

```

Which gives this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/1/3158e9cfc1e3952626c36d03c681f1ec3adc2f57.png)

But I'd like it in this format but only in one message. Your previous example through out this, everything above homeTeam: seems to be mangled somehow

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

---

<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:** [31 October 2021 22:42 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/9 "2021-10-31T22:42:29Z")

</div>

Just stick with JavaScript, its easier 😉 ...

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

```auto
[{"id":"f77a4f9882128f33","type":"inject","z":"af952aeaa20f4f97","name":"make request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":2490,"y":440,"wires":[["80e7042fb5cb18a1"]]},{"id":"80e7042fb5cb18a1","type":"http request","z":"af952aeaa20f4f97","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://www.statarea.com/predictions/date/2021-11-01/starttime","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":2670,"y":440,"wires":[["f7c6a7806f5a3f1d"]]},{"id":"f7c6a7806f5a3f1d","type":"html","z":"af952aeaa20f4f97","name":"EPL","property":"payload","outproperty":"payload","tag":"#\\30 10827","ret":"text","as":"multi","x":2830,"y":440,"wires":[["3a6afb35e994f108"]]},{"id":"3a6afb35e994f108","type":"change","z":"af952aeaa20f4f97","name":"Change","rules":[{"t":"change","p":"payload","pt":"msg","from":"your","fromt":"str","to":",your","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"prediction","fromt":"str","to":"prediction,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"LEAGUE","fromt":"str","to":"LEAGUE,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"TIP","fromt":"str","to":"TIP,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1X2H1HXH21.52.53.5BTSOTS","fromt":"str","to":",1X2H1HXH21.52.53.5BTSOTS,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"close","fromt":"str","to":",close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3020,"y":440,"wires":[["8f7cc712f4c3b994"]]},{"id":"8f7cc712f4c3b994","type":"change","z":"af952aeaa20f4f97","name":"Build array","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split(payload,',')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":2490,"y":500,"wires":[["19524db83822301c"]]},{"id":"40f4a17deb03a458","type":"debug","z":"af952aeaa20f4f97","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":3070,"y":500,"wires":[]},{"id":"19524db83822301c","type":"change","z":"af952aeaa20f4f97","name":"Change","rules":[{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[2]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"change","p":"payload[0]","pt":"msg","from":"ENGLAND","fromt":"str","to":"England","tot":"str"},{"t":"change","p":"payload[0]","pt":"msg","from":"PREMIER LEAGUE","fromt":"str","to":"Premier League","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":2660,"y":500,"wires":[["2d89cc6b0c38118f"]]},{"id":"2d89cc6b0c38118f","type":"function","z":"af952aeaa20f4f97","name":"nice-ification 😉","func":"let [countryLeague, data, stats] = msg.payload;\nlet countryLeagueParts = countryLeague.split(\" - \");\nlet country = countryLeagueParts[0].trim();\nlet league = countryLeagueParts[1].trim();\nlet number = (data.slice(0,2));\nlet date = new Date(data.slice(3,19));\nlet teams = data.slice(20).split(\"-\");\nlet statistics = stats.match(/.{1,2}/g)\nmsg.payload = {\n country,\n league,\n number,\n date,\n homeTeam: teams[0],\n awayTeam: teams[1],\n statistics: {\n \"home\": +statistics[0],\n \"draw\": +statistics[1],\n \"away\": +statistics[2],\n \"HT1\": +statistics[3],\n \"HTX\": +statistics[4],\n \"HT2\": +statistics[5],\n \"o1.5\": +statistics[6],\n \"o2.5\": +statistics[7],\n \"o3.5\": +statistics[8],\n \"bts\": +statistics[9],\n \"ots\": +statistics[10]\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2860,"y":500,"wires":[["40f4a17deb03a458"]]}]

```

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [31 October 2021 22:44 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/10 "2021-10-31T22:44:49Z")

</div>

Thankyou! 🙂

---

<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:** [31 October 2021 23:34 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/11 "2021-10-31T23:34:38Z")

</div>

Try this as some white space was not space

```auto
[{"id":"d88dd470.0ac7b8","type":"inject","z":"b779de97.b1b46","name":"make request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":140,"y":4900,"wires":[["874a3d4e.9b666"]]},{"id":"874a3d4e.9b666","type":"http request","z":"b779de97.b1b46","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://www.statarea.com/predictions/date/2021-11-01/starttime","tls":"","persist":false,"proxy":"","authType":"","x":140,"y":4950,"wires":[["a4684d13.279d9"]]},{"id":"a4684d13.279d9","type":"html","z":"b779de97.b1b46","name":"EPL","property":"payload","outproperty":"payload","tag":"div>[id='010827']","ret":"text","as":"multi","x":330,"y":4900,"wires":[["db4a77d111714b53"]]},{"id":"db4a77d111714b53","type":"change","z":"b779de97.b1b46","name":"Change","rules":[{"t":"change","p":"payload","pt":"msg","from":"your","fromt":"str","to":",your","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"prediction","fromt":"str","to":"prediction,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"LEAGUE","fromt":"str","to":"LEAGUE,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"TIP","fromt":"str","to":"TIP,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1X2H1HXH21.52.53.5BTSOTS","fromt":"str","to":",1X2H1HXH21.52.53.5BTSOTS,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"close","fromt":"str","to":",close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":4950,"wires":[["b45b7cdf7ba8e354"]]},{"id":"b45b7cdf7ba8e354","type":"change","z":"b779de97.b1b46","name":"Build array","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split(payload,',')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":4950,"wires":[["600f6ed3a3db3390"]]},{"id":"600f6ed3a3db3390","type":"change","z":"b779de97.b1b46","name":"Change","rules":[{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[2]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"change","p":"payload[0]","pt":"msg","from":"ENGLAND","fromt":"str","to":"England","tot":"str"},{"t":"change","p":"payload[0]","pt":"msg","from":"PREMIER LEAGUE","fromt":"str","to":"Premier League","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":490,"y":4900,"wires":[["463609ef.34eb38","242e92f4e3188b31"]]},{"id":"463609ef.34eb38","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"(\t $bits0 := $split(payload[0], \"-\").$trim($);\t $bits1 := [\t $append(\t $split(payload[1], /(-|\\s)+/)[[0..4]],\t $split(payload[1], \"-\")[[3,4]]\t )\t ].$trim($);\t $bits2 := [$match(payload[2], /\\d\\d/)].match;\t {\t \"country\": $bits0[0],\t \"league\": $bits0[1],\t \"number\": $bits1[0],\t \"date\": $bits1[1] & \"-\" & $bits1[2] & \"-\" & $bits1[3],\t \"time\": $bits1[4],\t \"timestamp\": $bits1[1] & \"-\" & $bits1[2] & \"-\" & $bits1[3] &\"T\" & $bits1[4] & \":00.000Z\",\t \"homeTeam\": $bits1[5],\t \"awayTeam\": $bits1[6],\t \"home\": $bits2[0],\t \"draw\": $bits2[1],\t \"away\": $bits2[2],\t \"HT1\": $bits2[3],\t \"HTX\": $bits2[4],\t \"HT2\": $bits2[5],\t \"o1.5\": $bits2[6],\t \"o2.5\": $bits2[7],\t \"o3.5\": $bits2[8],\t \"bts\": $bits2[9],\t \"ots\": $bits2[10]\t }\t\t)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":720,"y":4960,"wires":[["609933cde0b38546"]]},{"id":"242e92f4e3188b31","type":"function","z":"b779de97.b1b46","name":"","func":"let [countryLeague, data, id] = msg.payload;\nlet countryLeagueParts = countryLeague.split(\" - \");\nlet country = countryLeagueParts[0].trim();\nlet league = countryLeagueParts[1].trim();\nlet number = (data.slice(0,2));\nlet date = new Date(data.slice(3,19));\nlet teams = data.slice(20).split(\"-\");\n\nmsg.payload = {\n country,\n league,\n number,\n date,\n homeTeam: teams[0],\n awayTeam: teams[1],\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":710,"y":4900,"wires":[["04848441442b51d5"]]},{"id":"609933cde0b38546","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":960,"y":4970,"wires":[]},{"id":"04848441442b51d5","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":4900,"wires":[]}]

```

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [1 November 2021 13:28 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/12 "2021-11-01T13:28:36Z")

</div>

That worked great for that data, it seems there's a slight variation in the input text for different games, this is for the number see at the start of the second line in the array. This number could be either 1 or 2 or X or 1X or X2

This is what I currently have which is missing the date

```auto
[{"id":"f77a4f9882128f33","type":"inject","z":"ffe44f33e5843155","name":"make request","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payloadType":"date","x":380,"y":570,"wires":[["80e7042fb5cb18a1"]]},{"id":"80e7042fb5cb18a1","type":"http request","z":"ffe44f33e5843155","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"http://www.statarea.com/predictions/date/2021-11-05/starttime","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":560,"y":570,"wires":[["f7c6a7806f5a3f1d"]]},{"id":"f7c6a7806f5a3f1d","type":"html","z":"ffe44f33e5843155","name":"EPL","property":"payload","outproperty":"payload","tag":"#\\30 10827","ret":"text","as":"multi","x":720,"y":570,"wires":[["3a6afb35e994f108"]]},{"id":"3a6afb35e994f108","type":"change","z":"ffe44f33e5843155","name":"Change","rules":[{"t":"change","p":"payload","pt":"msg","from":"your","fromt":"str","to":",your","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"prediction","fromt":"str","to":"prediction,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"LEAGUE","fromt":"str","to":"LEAGUE,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"TIP","fromt":"str","to":"TIP,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"1X2H1HXH21.52.53.5BTSOTS","fromt":"str","to":",1X2H1HXH21.52.53.5BTSOTS,","tot":"str"},{"t":"change","p":"payload","pt":"msg","from":"close","fromt":"str","to":",close","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":910,"y":570,"wires":[["8f7cc712f4c3b994"]]},{"id":"8f7cc712f4c3b994","type":"change","z":"ffe44f33e5843155","name":"Build array","rules":[{"t":"set","p":"payload","pt":"msg","to":"$split(payload,',')","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":630,"wires":[["19524db83822301c"]]},{"id":"40f4a17deb03a458","type":"debug","z":"ffe44f33e5843155","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":960,"y":630,"wires":[]},{"id":"19524db83822301c","type":"change","z":"ffe44f33e5843155","name":"Change","rules":[{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[0]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[1]","pt":"msg"},{"t":"delete","p":"payload[2]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"delete","p":"payload[3]","pt":"msg"},{"t":"change","p":"payload[0]","pt":"msg","from":"ENGLAND","fromt":"str","to":"England","tot":"str"},{"t":"change","p":"payload[0]","pt":"msg","from":"PREMIER LEAGUE","fromt":"str","to":"Premier League","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":550,"y":630,"wires":[["2d89cc6b0c38118f","8a86d95287f3cbc4"]]},{"id":"2d89cc6b0c38118f","type":"function","z":"ffe44f33e5843155","name":"nice-ification 😉","func":"let [countryLeague, data, stats] = msg.payload;\nlet countryLeagueParts = countryLeague.split(\" - \");\nlet country = countryLeagueParts[0].trim();\nlet league = countryLeagueParts[1].trim();\nlet number = (data.slice(0,2));\nlet date = new Date(data.slice(3,19));\nlet teams = data.slice(20).split(\"-\");\nlet statistics = stats.match(/.{1,2}/g)\nmsg.payload = {\n country,\n league,\n number,\n date,\n homeTeam: teams[0],\n awayTeam: teams[1],\n statistics: {\n \"home\": +statistics[0],\n \"draw\": +statistics[1],\n \"away\": +statistics[2],\n \"HT1\": +statistics[3],\n \"HTX\": +statistics[4],\n \"HT2\": +statistics[5],\n \"o1.5\": +statistics[6],\n \"o2.5\": +statistics[7],\n \"o3.5\": +statistics[8],\n \"bts\": +statistics[9],\n \"ots\": +statistics[10]\n }\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":750,"y":630,"wires":[["40f4a17deb03a458"]]},{"id":"8a86d95287f3cbc4","type":"debug","z":"ffe44f33e5843155","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":680,"wires":[]}]

```

Input

```auto
array[3]
0: " England - Premier League"
1: "1 2021-11-05 20:00-Southampton-Aston Villa"
2: "4725283343248257395743"

```

Output

```auto
object
country: "England"
league: "Premier League"
number: "1 "
date: null
homeTeam: "outhampton"
awayTeam: "Aston Villa"
statistics: object
home: 47
draw: 25
away: 28
HT1: 33
HTX: 43
HT2: 24
o1.5: 82
o2.5: 57
o3.5: 39
bts: 57
ots: 43

```

The problem I see here is the first number on line 2 of the imput, on the previous input this was two digits long, this time it's only one so I'm guessing that it breaks the rest of the formatting for this line?

---

<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:** [1 November 2021 14:33 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/13 "2021-11-01T14:33:14Z")

</div>

It can be solved fairly simply - I don't have time right now but will try to help out later tonight

---

<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:** [1 November 2021 18:25 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/14 "2021-11-01T18:25:24Z")

</div>

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

Try changing the function code to this...

```javascript
const [countryLeague, data, stats] = msg.payload;
const countryLeagueParts = countryLeague.split(" - ");
const dataParts = data.match(/(.+?)\W([\d-]+\W[\d:]+)-(.+?)-(.+)/);
const statistics = stats.match(/.{1,2}/g)
msg.payload = {
    country: countryLeagueParts[0].trim(),
    league: countryLeagueParts[1].trim(),
    number: dataParts[1],
    date: new Date(dataParts[2]),
    homeTeam: dataParts[3],
    awayTeam: dataParts[4],
    statistics: {
        "home": +statistics[0],
        "draw": +statistics[1],
        "away": +statistics[2],
        "HT1": +statistics[3],
        "HTX": +statistics[4],
        "HT2": +statistics[5],
        "o1.5": +statistics[6],
        "o2.5": +statistics[7],
        "o3.5": +statistics[8],
        "bts": +statistics[9],
        "ots": +statistics[10]
    }
}
return msg;

```

---

<div class="post-metadata">

**Author:** ![Romprod](https://avatars.discourse-cdn.com/v4/letter/r/e47774/32.png) [@Romprod](https://discourse.nodered.org/u/Romprod)\
**Post date:** [1 November 2021 22:00 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/15 "2021-11-01T22:00:07Z")

</div>

Perfect thank you.

That works across a whole bunch of games and leagues! 🙂

My next task is learning how to write all of this to a database and calling the details in to Telegram!

---

<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:** [15 November 2021 22:00 UTC](https://discourse.nodered.org/t/working-with-data-in-an-array/53004/16 "2021-11-15T22:00:56Z")

</div>

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