Best answer

Using Javascript to count line items

  • 25 June 2021
  • 1 reply
  • 329 views

Greetings!

 

I am using a small piece of javascript code to count the numbers of items in a comma seperated list. The code looks like this:

 

let items = inputData.names.split(',');
let length = items.length;
output = [{length: length}];

 

This works great in cases where the list has at least 1 item. However, sometimes the input field will be empty, and then I run into an error. 

 

Is there a way to modify this code so that if the list contains no elements it returns 0, and counts as normal when the list contains items?

 

Thanks!

icon

Best answer by ikbelkirasan 26 June 2021, 00:13

View original

This post has been closed for comments. Please create a new post if you need help or have a question about this topic.

1 reply

Userlevel 7
Badge +12

Hi @Ahmad K - Sure thing! Here’s an example:

let items = [];
if (inputData.names) {
items = inputData.names.split(",");
}
return [{ length: items.length }];