Array of strings to numbers and naming them

Hello Everyone, I working with a node which outputs an array of 8 strings and I wish to convert these to numbers and name each of the elements in the array. The elements of the array always arrive in the same order. I use the buffer/parser node in my modbus projects to achieve this goal but I don't know how to achieve the same thing here. Any suggestions?

First thing to do is a google search (try javascript convert array of strings to numbers) and see if what you find will solve your problem!

I would look to maybe use a join node in manual mode to join the array elements into a CSV string - and then into a csv node with a template set to the names you want and set to parse numeric values.

1 Like

Or a quick little function node:

let stringArray = msg.payload;
let numberArray = stringArray.map(Number);
msg.payload = numberArray;
return msg;

That doesn't achieve the naming of the elements....

Missed that in reading the thread (need that coffee to kick in)

@GRHMLGGT when you say you want to 'name each of the elements in the array' do you mean you want each element of the array to be an object (i.e.

[{"item1",15.9337},{"item2",-772.0405}....]

or something else? Can you show whatt he output should look like?

I think you meant ...em1":15... -- def need that coffee !

the CSV node would give you {"item1":15.9337,"item2":-772.04505 ... etc } ie just an object with named properties... not an array... so yes depends what @GRHMLGGT really requires.

1 Like

Thanks, I already know that I can use a simple bit of JSONata to so this, but to do it and name all the elements is what I am not sure about.

Here is what I want, this comes from using the buffer/parser node on a modbus project I done sometime ago. Which is each element becomes an object.
Screenshot 2022-11-16 115439

So yes the CSV node can do that for you if you feed it appropriately

1 Like

The JSONata expression would be like below except the names array would have 8 elements

(
    $names := ["one","two"];
    $$.payload#$i.${$names[$i]: $number($)}
)
1 Like

Function node would be like this:

let stringArray = msg.payload;
let numberArray = stringArray.map(Number);
let resultobj = {
    "thing0": numberArray[0],
    "thing1": numberArray[1],
    "thing2": numberArray[2]
}
msg.payload = resultobj;
return msg;

You will have to add the other names in the code.

1 Like

All the solutions offered will do the job! Thanks again for the swift responses.