Split values in a string?

I got some values in a string. I need to split them as i want to find the average of them.
Skjermbilde 2021-10-30 kl. 14.51.10

Every number contains of 5 digits: 0.1263 and so on. Anyone who can help me with this? It will be much appreciated.

Freddy.

In a function node use match().

msg.payload = msg.payload.match(/\d\.\d\d\d\d/g);
return msg;

Thank you :slight_smile: I have a lot of basic to learn, but this works like a charm.

image

What about this number ?
is it 0.174 ? or 0.1740 ?
how does regex handle that?
do we miss the next value 0.1628 ?

ps. whats producing that string? can you edit the source and seperate the numbers with comma or ; ?

you could use this if there are numbers with more or less digits.
msg.payload.match(/\d\.\d+(?=(\d\.|$))/g)
Or do the whole thing in a change node and output the average.
e.g.

[{"id":"3c2e26b8.09178a","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"0.12340.23450.6780.34560.4567","payloadType":"str","x":170,"y":3900,"wires":[["d6d52728.f577a"]]},{"id":"d6d52728.f577a","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"  $match($$.payload, /\\d\\.\\d+(?=(\\d\\.|$))/).$number($.match)\t","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":360,"y":3900,"wires":[["d9a3223d.b2b658"]]},{"id":"d9a3223d.b2b658","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":3900,"wires":[]}]
$average(
   $match($$.payload, /\d\.\d+(?=(\d\.|$))/).$number($.match)
)

Every value starts with 0. This is data sent me from another provider. I had put some code in a function and that was the result. But anyway the first solution provided here, worked very well.

This is what i got with the first solution. Three digits value got a 0 added, but that is ok, and more than good enough.

Look at 19 and 20, there is one missing between there, because the 0.174 is missing a digit in the source.

Yes you are correct I am missing one value. I just didnt see it because the average got correct. I was searching for the missing value right now, but i guess you found it first.

I didn't need to look for it, it had already been pointed out the first solution would only work with fixed length strings, which is what you said you had in the first post, but in fact they are not fixed length.

1 Like

This is from the source. I have tried to isolate only the "total" as its what i need. But since I am new to this i edited in someone elses function-node (copy) and i got that string. There is I think... a better solution.

image

This is the function causing the string.

First, please don't post code as an image. Use the method described in How to share code or flow json. That would have saved me having to type out bits of the code again.

Change
let data = ""
which initialises data to an empty string so that when you add the other ones it concatenates the text rather than adding the numbers
to
let data = 0

In fact even better would be to use Array method reduce(). See Array.prototype.reduce() - JavaScript | MDN

Thank you :slight_smile: I will try to post codes correctly. And thanks for the tips to find the solution. I will look into that.

let pl = msg.payload;

let prices = pl.viewer.homes[0].currentSubscription.priceInfo.today;

// Meassurement
let data = "";

for(let i = 0; i < prices.length; i++)
{
    let p = prices[i];
   
    
    // Field set
    data += + p.total;
}

msg.payload = data; 

return msg;

As you have a original object containing all the info, it would be simpler to do it all in the function to get an average. I'm sure others here would help there.
You could also do it in a change node with a JSONata expression using the object shown in previous images.

$average(payload.viewer.homes.currentSubscription.priceInfo.today.total)

example flow

[{"id":"3602fd47.c83062","type":"inject","z":"b779de97.b1b46","name":"","props":[{"p":"payload.viewer.homes.currentSubscription.priceInfo.today","v":"[{\"total\":0.1234},{\"total\":0.2345},{\"total\":0.678},{\"total\":0.3456},{\"total\":0.4567}]","vt":"json"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","x":160,"y":3860,"wires":[["542ca9ea.f60988","4521ee14.b992c8"]]},{"id":"542ca9ea.f60988","type":"change","z":"b779de97.b1b46","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$average(payload.viewer.homes.currentSubscription.priceInfo.today.total)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":380,"y":3860,"wires":[["4521ee14.b992c8"]]},{"id":"4521ee14.b992c8","type":"debug","z":"b779de97.b1b46","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":620,"y":3860,"wires":[]}]

Thank you very much E1cid, got it solved thanks to you. Probably it could have been more elegant, but it works :slight_smile:

Make sure you understand how the solution works, otherwise you will not have learnt from this.

I try, I always do.

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