• Instructions to Ask a Question

    Click on the "Ask a Question" button and select the application for which you would like to ask questions.

    We have 5 different products namely - Pabbly Connect, Pabbly Subscription Billing, Pabbly Email Marketing, Pabbly Form Builder, Pabbly Email Verification.

    The turnaround time is 24 hrs (Business Hours - 10.00 AM to 6.00 PM IST, Except Saturday and Sunday). So your kind patience will be highly appreciated!

    🚀🚀Exclusive Discount Offer

    Just in case you're looking for any ongoing offers on Pabbly, you can check the one-time offers listed below. You just need to pay once and use the application forever -
     

    🔥 Pabbly Connect One Time Plan for $249 (🏆Lifetime Access) -  View offer 

    🔥 Pabbly Subscription Billing One Time Plan for $249 (🏆Lifetime Access) - View offer

Iterator iteration control

JeffB

Member
I have an ITERATOR action in my workflow. Following the ITERATOR action, I have a FILTER to filter only on "Seattle". If the city is Seattle, I have 5 steps I want to perform for each iteration. So my workflow looks like this:

ITERATOR -> FILTER -> Action 1 -> Action 2 -> Action 3 -> Action 4 -> Action 5.

My data looks like this:
[
{
"city": "Los Angeles",
"population": 300,
"males": 150,
"females": 150
},
{
"city": "Seattle",
"population": 300,
"males": 150,
"females": 150
},
{
"city": "Seattle",
"population": 300,
"males": 150,
"females": 150
},
{
"city": "Boston",
"population": 300,
"males": 150,
"females": 150
}
]

I only want to process only the 1st "Seattle" array element. Here's my question: after I receive the first iteration of "Seattle", I want to ignore the rest of the array elements.

I think I can do this with a counter and a filter, but I'm not sure.

If I change my workflow to this:

ITERATOR -> FILTER (If city = Seattle) -> COUNTER (initial value = 0, increment by 1, final value = 1, Reset = No) -> Filter (If final value = 1) -> Action 1 -> Action 2 -> Action 3 -> Action 4 -> Action 5.

It is my believe that this workflow will process all array elements via ITERATOR, but will only pass the 1st FILTER if the city is Seattle. And will only pass the 2nd filter if the COUNTER final value is 1 (one), and then of course the 5 actions I want to happen will only happen 1 time, because Seattle was only found 1 time while the counter was 1. I assume that the 2nd time ITERATOR finds "Seattle", it will pass the 1st FILTER. COUNTER will then increment the final value by 1 (1 + 1) and it will now have a value of 2. The 2nd FILTER will not pass because the final value is no longer 1.

Is this correct?
 

Fagun Shah

Well-known member
Instead of looping/iterating through each item in array use Code - JS Code action to get the first occurrence where city is "Seattle"

const data = [
{
"city": "Los Angeles",
"population": 300,
"males": 150,
"females": 150
},
{
"city": "Seattle",
"population": 300,
"males": 150,
"females": 150
},
{
"city": "Seattle",
"population": 400,
"males": 250,
"females": 250
},
{
"city": "Boston",
"population": 300,
"males": 150,
"females": 150
}
];

const seattleData = data.find(obj => obj.city === "Seattle");

return seattleData;

1710044415481.png
 

JeffB

Member
Hey @Fagun Shah, that worked perfectly! Thanks so much. I see a ton of uses for this kind of thing, but my JS coding is lacking. If I run into issues, I'll post them and if you're available for suggestions, I'll listen to every one!

Thanks again.
 
Top