Split String to Array

Hello.

Can someone help me split this


[
{
"values": "111, 222, 333"
}
] 

into this


[
{
"values": "111"
},
{
"values": "222"
},
{
"values": "333"
}
] 

You could do so via a short JS snippet using String.split().

Example Workflow

image

This example would also work for multiple input items with an array in each. If you ever only have one input item you can simplify it a bit as you wouldn’t need to loop through multiple items.

3 Likes

Thank you very much.

It works.

1 Like

Thank you for the solution. Expanding @Mulen example, I have a query, is there a way to add other keys to the same referenced “values” and club them under their respective object?

[
{
"values": "111, 222, 333",
"names": "A, B, C"
}
] 

into this:

[
{
"value": "111",
"name": "A"
},
{
"value": "222",
"name": "B"
},
{
"value": "333",
"name": "C"
}
]

Check it out:

3 Likes

Thank you!

2 Likes

Hello @Shirobachi

Is there a way to call the items of values and names that are to be split from a remote node (node not connected in order)?

let myValues = [];
let myNames = [];

for (item of items) {
  myValues.push(...item.json.values.split(','));
  myNames.push(...item.json.names.split(','));
}

let res = [];

// use max of both
for(i=0; i<myNames.length; i++){
  res.push(
    {
      "json": {
        "value": myValues[i],
        "name": myNames[i]
      }
    }
  )
}

return res

I think it is not possible, but maybe someone more experienced will confirm this.

For sure, you can call value from other node if there are connected like this: