# Array of strings to numbers and naming them

**URL:** <https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700>\
**Category:** General\
**Created:** [16 November 2022 11:00 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700 "2022-11-16T11:00:22Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![GRHMLGGT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grhmlggt/32/59269_2.png) [@GRHMLGGT](https://discourse.nodered.org/u/GRHMLGGT)\
**Post date:** [16 November 2022 11:00 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/1 "2022-11-16T11:00:23Z")

</div>

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?

 ![Screenshot 2022-11-16 105857](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dbf19a6851c98327d8ce241b48d64db25dee7923.png)

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 November 2022 11:16 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/2 "2022-11-16T11:16:26Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 November 2022 11:18 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/3 "2022-11-16T11:18:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 November 2022 11:28 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/4 "2022-11-16T11:28:22Z")

</div>

Or a quick little function node:

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

```

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 November 2022 11:30 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/5 "2022-11-16T11:30:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 November 2022 11:35 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/6 "2022-11-16T11:35:41Z")

</div>

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.

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

```

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

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 November 2022 11:44 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/7 "2022-11-16T11:44:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GRHMLGGT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grhmlggt/32/59269_2.png) [@GRHMLGGT](https://discourse.nodered.org/u/GRHMLGGT)\
**Post date:** [16 November 2022 11:53 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/8 "2022-11-16T11:53:08Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![GRHMLGGT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grhmlggt/32/59269_2.png) [@GRHMLGGT](https://discourse.nodered.org/u/GRHMLGGT)\
**Post date:** [16 November 2022 11:56 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/9 "2022-11-16T11:56:10Z")

</div>

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](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/f/df87369e0393b0835dbe5bdf233ce7bb4a4804d4.png)

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [16 November 2022 12:10 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/10 "2022-11-16T12:10:30Z")

</div>

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

---

<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:** [16 November 2022 12:17 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/11 "2022-11-16T12:17:45Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [16 November 2022 12:18 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/12 "2022-11-16T12:18:46Z")

</div>

Function node would be like this:

```auto
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.

---

<div class="post-metadata">

**Author:** ![GRHMLGGT](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/grhmlggt/32/59269_2.png) [@GRHMLGGT](https://discourse.nodered.org/u/GRHMLGGT)\
**Post date:** [16 November 2022 13:20 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/13 "2022-11-16T13:20:17Z")

</div>

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

---

<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:** [30 November 2022 13:20 UTC](https://discourse.nodered.org/t/array-of-strings-to-numbers-and-naming-them/70700/14 "2022-11-30T13:20:46Z")

</div>

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