Skip to content
Advertisement

Nested loop returns last 2 values

I’m trying to send multiple items to multiple contacts in my contacts list , I use nested loop but it only send the last item for each contact ignoring the rest of the items , I didn’t know what I’m doing wrong.

here are my arrays :

JavaScript

here is my JS code :

JavaScript

Advertisement

Answer

Create an array messagesToSend = [] outside the second loop and then inside the 2nd loop push your object in that variable messagesToSend.push(this.extractMessageDetails(this.ContactList[i], this.itemList[j]););. So in the end of your second loop you will have messages for each contact

JavaScript

If you want to collect all messages for all contracts move declaration of array outside the loops

Advertisement