# Help with loop in a function

How can I loop through all IDs?
This is an example I found on the net and tried to modify.

``````let id1 = 111111111; //dejan
let id2 = 22222222; //lepi
let id3 = 33333333; //milos
let id4 = 44444444; //recepcija
let id5 = 55555555; //miljan
let id0 = 66666666; //marina

count=0;
for(var i=0;i<5;i++)
{
"chatId": id[i],
"messageId": 1,
"type": "message",
}
node.send(msg)
count+=1;
}
``````

would be better having chatId's in an array

``````let id = [111111111, //dejan
22222222, //lepi
33333333, //milos
44444444, //recepcija
55555555, //miljan
66666666]; //marina

count=0;
for(var i=0;i<5;i++)
{
"chatId": id[i],
"messageId": 1,
"type": "message",
}
node.send(msg)
count+=1;
}
return null;
``````

But you could use eval()

``````let id1 = 111111111; //dejan
let id2 = 22222222; //lepi
let id3 = 33333333; //milos
let id4 = 44444444; //recepcija
let id5 = 55555555; //miljan
let id0 = 66666666; //marina

count=0;
for(var i=0;i<5;i++)
{
"chatId": eval("id" + i),
"messageId": 1,
"type": "message",
}
node.send(msg)
count+=1;
}
return null;
``````
1 Like

I went with an array

``````let id = [ 518xxxx212, //dejan
523xxxx789, //lepi
145xxxx930, //milos
102xxxx503, //miljan
537xxxx043,] //marina

for(var i=0;i<5;i++)
{
"chatId": id[i],
"messageId": 1,
"type": "message",
"content": (operater+"\n"+"Kvar:\n"+ kvar),
}
node.send(msg)

}
return null;

``````

Thanks E1cid

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