How to return all the items in Code node (Run Once for Each Item)

Hi, I am new in n8n as well as Javascript so I’m a little stuck at this.

I have a Code node that capitalizes the first character of every word. However, it only works for one item instead of multiple ones. I figure I need to change from “Run Once for All Items” to “Run Once for Each Item”.

How can I make it returns all the items?

The code:

// Get the Text and store into a variable called myText
const myText = $node["IF (Check Bank)"].json["Email Body"].toLowerCase();

// Now using Regex we can replace every first letter of the Word to Uppercase
const EmailBody = myText.replace(/(^\w|\s\w)/g, m => m.toUpperCase());

// Now return (output) the converted text
return [{
  json: { EmailBody }
}]

Many thanks.

I’m thinking a Set Node would work for this specific example, with a fancy expression like so, assuming toTitleCase() formats the text like you wanted.

{{ $json["Email Body"].toLowerCase().toTitleCase() }}

1 Like

Actually, I think this is the corrected code for your Code Node with “Run Once For Each Item”

// Get the Text and store into a variable called myText
const myText = $node["If (Check Bank)"].json["Email Body"].toLowerCase();

// Now using Regex we can replace every first letter of the Word to Uppercase
const EmailBody = myText.replace(/(^\w|\s\w)/g, m => m.toUpperCase());

// Now return (output) the converted text
return {
  json: { "Email Body": EmailBody }
};
1 Like

This is exactly what I need! Thanks so much. I didn’t know we have “toTitleCase()”. Thank you for your updated code for the Code Node too.

1 Like

I don’t seem to get the .toTitleCase() to work. Did you install anything beforehand? Thanks

I don’t have anything extra installed. I trust your n8n version is pretty recent but if possible, you may want to update it if it’s not.

Do you mean that it does work without the toTitleCase ?

The Set node is definitely more convenient to work with, but at least the Code node is a working option for you (I hope).

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