Share feedback, ideas and get community help

D
F
M
a
g

Strange behavior with arrays

This code returns a list starting from 1. Strange:

const itens = {{categoriesListName}}; let list = itens.map((item, index) => `${index}. ${item}`).join('\n'); setVariable('categoriesListChoice', list);

If use {index-1} this code returns a list starting from -1!!! Very Strange.
1
M
O
B
20 comments
i think that you want the list to start at 1 instead of 0, you can add 1 to index. try with this :

const itens = {{categoriesListName}};
let list = itens.map((item, index) => ${index + 1}. ${item}).join('\n');
setVariable('categoriesListChoice', list);
No, I want it to start from 0. The script is supposed to work like this, but strangely it starts from 1.
When I try to subtract, it starts from -1. Even stranger
I get the feeling that the index is manipulated before it is run.
mmhh strange but have you already tried to log the index so to verify if is really manipulated or not?

const itens = {{categoriesListName}};
itens.forEach((item, index) => console.log(Index: ${index}, Item: ${item}));

let list = itens.map((item, index) => ${index}. ${item}).join('\n');
console.log(list);
setVariable('categoriesListChoice', list);
I didn't log it, but I do display it in the chat, so I know.
Console.log doesn't work in Typebot, according to its own documentation. I'm using the free web version for now.
really so strange.
I'm not expert but i know just a little that you asked and for this reason i've tried to help you.
Look like that's Typebot to manipulate the index so, only @Baptiste can give you a correc reply or someone that has had the same issue.
Sorry to have not helped you 😦
To add to the strangeness, last week the index started at 0. It appears to have been an update.
I am not sure to understand
Can you describe the issue following this?

  1. Steps to reproduce
  2. What's happening
  3. What were you expecting
Create a script using the map function to return the array index in the conversation.
Like the example above.

It's returning the wrong index, starting at 1 instead of 0.

And if you subtract the index it returns -1 in the conversation.

@Baptiste
Your code is invalid it should be surrounded with ` instead of "
Plain Text
const itens = ["test", "test2"]
let list = itens.map((item, index) => `${index}. ${item}`).join('\n');
setVariable('List', list);
This works just fine
No, I changed the quotes " in my first message because the code quoting here on Discord was out of format. I changed it now.
Question 1: Shouldn't the array start at 0?
Question 2: Try subtracting the index and making it start at zero. I can't, because when I try that, it starts at -1.
@Baptiste to help provide an example
Attachment
image.png
Oh right, that’s because Typebot detects it as a list and display a list in the text bubble
And a list always starts with 1
@Baptiste This answers another problem I have here and was trying to figure out, see:

let numbers = ['Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten']; let start = 5; let listText = numbers.map((item, index) => `${start + index}. ${item}`).join('\n'); setVariable('VariableList', listText);

I need a list to be incremented with each new request, so each new list that I present in this format must continue the previous numbering, but this is not possible, because every list that I present in the conversation is restarted to start at 1!
See the image.

How do I solve this?
Attachment
image.png
I did a test here and discovered that the notation "1. Item 1" is being interpreted as a list and always restarts from 1.
Therefore, not using the point "." seems to solve the problem 🫤
Add a reply
Sign up and join the conversation on Discord
Join