Share feedback, ideas and get community help

Updated 3 days ago

Script: How to list array ?

At a glance

A community member is trying to display an array of data in a list format, but is having trouble with the code. Other community members provide suggestions, including using the .map() method to create a list with each item on a new line. The final solution is to use {{servicos}}.map(item => `- ${item}`).join('\n') to display the array elements as a bulleted list, or {{servicos}}.map((item, index) => `${index + 1}. ${item}`).join('\n') to display a numbered list. The community members work together to troubleshoot and find a working solution.

Hi! Someone help me this plz...
i try:
Plain Text
for (let index = 0; index < $servicos.lenght ; index++) {
  return {{$servicos[index]}};
}


but don't worked...
Marked as solution
Okay I see now. Here's a one-liner to do just that:
Plain Text
{{servicos}}.map(item => `- ${item}`).join('\n')

From an array that looks like item1, item2, item3, it will return:
Plain Text
- item1
- item2
- item3

Try it and let me know if it worked for you
View full solution
A
N
27 comments
Hello Nomad, can you give some context about what you're trying to do and why?
Your post title says "how to list array": are you willing to just show to the user the elements in a list separated by a comma as a string? If so, you should just be able to call your array variable in a text bubble {{your_array}} and it would display something like item1, item2, item3...
Hi! My goal is to return the array data in list form.
Example:
  • Item one
  • Item two
  • Item...
Okay I see now. Here's a one-liner to do just that:
Plain Text
{{servicos}}.map(item => `- ${item}`).join('\n')

From an array that looks like item1, item2, item3, it will return:
Plain Text
- item1
- item2
- item3

Try it and let me know if it worked for you
Can confirm it works. In my example, I use the inline syntax in a text bubble
Plain Text
{{={{servicos}}.map(item => `- ${item}`).join('\n')=}}
Attachment
image.png
ty for help, but '\n' don't work on watsapp =/
It worked in WhatsApp with my flow above
Attachment
image.png
wow, i try here again
is possible to substitute "-" for index number ?
So you want to make it a numbered list instead of a bullet points list, so here's the updated code:
Plain Text
{{servicos}}.map((item, index) => `${index + 1}. ${item}`).join('\n')
ty, i try again
Here's how it looks in WhatsApp
Attachment
image.png
ReferenceError: index is not defined
Show me a screenshot of your Typebot flow if you can, that'll help
And also an example of your array content or format/syntax
sry, my fault, try again
worked on whasapp but index return all 1
How does your array look like?
I mean what you have in {{servicos}} before applying the script
but on typebot works
Attachment
image.png
Just like I did in my flow, add a text bubble with just {{servicos}} in it and show me what it displays in chat
Also, you seem to have edited my code slightly, as in mine I added a space between the number and dot, and the item, such as 1. Item instead of yours 1.Item
Was it because of the space character? I'm glad it works! πŸ™‚
Nice buddy, though I think it'll look better with a space after the . after the number
It's strange that a space could cause this, but if the solution is to remove it, so be it XD
Add a reply
Sign up and join the conversation on Discord