foreach into another foreach

Hello,

I have 3 arrays: 1-I get from service, 2-static data, 3 - new array that I want to show in client side). I need to check every from array 1 object  if it exists in array 2, if yes - add object to array 3. For example:

responseArr = [1,2,3,4];
staticArr = [2,3,5,6];
newArr=[];
responseArr.forEach(function(objResp) {
staticArr.forEach(function(objStatic) {
  if (objResp== objStatic) {
   newArr.push(objResp)
   } }}

So I have a problem to build this function in my preparation. Attached preparation that I built.

Somebody can help me?




Hi, 


the flow is incorrect,




Best regards

Viktoria Yosupova wrote:

Hello,

I have 3 arrays: 1-I get from service, 2-static data, 3 - new array that I want to show in client side). I need to check every from array 1 object  if it exists in array 2, if yes - add object to array 3. For example:

responseArr = [1,2,3,4];
staticArr = [2,3,5,6];
newArr=[];
responseArr.forEach(function(objResp) {
staticArr.forEach(function(objStatic) {
  if (objResp== objStatic) {
   newArr.push(objResp)
   } }}

So I have a problem to build this function in my preparation. Attached preparation that I built.

Somebody can help me?





Hello Viktoria 

I believe you can do it with one loop. 

Psuedo code 

foreach (element in List1)

{

   List2.ListIndexof() or List2.Filter() // condition to check if the second list has the element 

  If the element exists in List2

     List3.add(element) 

}


Let me know if this helps 


Its not work :(

Amal Raj wrote:

Viktoria Yosupova wrote:

Hello,

I have 3 arrays: 1-I get from service, 2-static data, 3 - new array that I want to show in client side). I need to check every from array 1 object  if it exists in array 2, if yes - add object to array 3. For example:

responseArr = [1,2,3,4];
staticArr = [2,3,5,6];
newArr=[];
responseArr.forEach(function(objResp) {
staticArr.forEach(function(objStatic) {
  if (objResp== objStatic) {
   newArr.push(objResp)
   } }}

So I have a problem to build this function in my preparation. Attached preparation that I built.

Somebody can help me?





Hello Viktoria 

I believe you can do it with one loop. 

Psuedo code 

foreach (element in List1)

{

   List2.ListIndexof() or List2.Filter() // condition to check if the second list has the element 

  If the element exists in List2

     List3.add(element) 

}


Let me know if this helps 


Hi , 

Did you try my approach, are you facing any issues with it ?


Regards



Hi Viktoria,


From what I see it seems there is something missing, check picture below.

Best Regards,

Jorge Cerveira


Hi Viktoria,

Every Statement (Assignment, For Each, Action etc.) needs an input and an output connector. A For Each has two input connectors and two output connectors:

  1. The normal input connector leading to the For Each;
  2. The Cycle output connector that is taken for every element in the Record List Parameter;
  3. The input connector leading back from the Cycle;
  4. The normal output connector leading to the next Statement.

In your first example, the output connector of Assignment 3 leads back to the outer For Each, instead of the inner one that it's part of, and the inner For Each doesn't have an output connector. In your second example, though the connector from the Assign is now correct, the inner For Each still doesn't have an output, and the outer For Each also doesn't have an input back from the Cycle.

Also note that the True Change tab in shows exactly what the problems are, and when hovering the mouse over the For Each with the red squiggly line, Service Studio also shows exactly what the error is.

Hello Amal,


I did it with ListFilter and add new children to new array, but now I have another problem: I see in new array items with 0 or "" property ([3]:{id: 0, name:"", imgSrc:""}), it item that did not pass in condition of filter. How can I add just relevant items?


Thank you

Viktoria Yosupova wrote:

Hello Amal,


I did it with ListFilter and add new children to new array, but now I have another problem: I see in new array items with 0 or "" property ([3]:{id: 0, name:"", imgSrc:""}), it item that did not pass in condition of filter. How can I add just relevant items?


Thank you

Hi Viktoria, 

You can check with an IF condition if there are any items that are filtered  and then add only the relevant items to the new list. You can create a local variable, populate it and then append it to the final list. 

If there are no items that are filtered then skip adding (appending) them and iterate to the next element in the loop. 

Hope this helps !!!


Hi Amal, Viktoria,

A ListAppendAll with as source an empty list does not add anything to the destination list! So a LisAppendAll with as source the output of a ListFilter shouldn't need an If to check whether it's empty!