cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Filter function in snaplogic

Anjali
New Contributor

Can anyone make me understand what the below function is doing?

filter((a, b, c) => c.indexOf(a) == b)

4 REPLIES 4

RogerSramkoski
Employee
Employee

The filter() function is taking (a, b, c) as parameters and everything after => is an โ€œon the flyโ€ function to help determine which values from (a, b, c) should be in the output. If Iโ€™m not mistaken, the function is searching string โ€˜cโ€™ to see if it contains string โ€˜aโ€™, and if it so, does that equal integer โ€˜bโ€™. Basically, if string โ€˜aโ€™ is detected inside string โ€˜cโ€™ at the expected location โ€˜bโ€™, then the document would pass through the filter, otherwise it would be excluded.

In SnapLogic you can use filter() on Objects and Arrays. If youโ€™re able to share more context around the expression such as sample data, or all/part of a pipeline that uses it, we can try to get you more information.

Please let us know if that helps and if some part still needs clarified, please let us know.

To remove duplicate entries in an array:

Expression : $myarray.filter((item, pos, a) => a.indexOf(item) == pos)

where $myarray is a normal array containing [โ€œFredโ€, โ€œWilmaโ€, โ€œFredโ€, โ€œBettyโ€, โ€œFredโ€, โ€œBarneyโ€]

Result : A new array containing [Fred, Wilma, Betty, Barney].

I found this example in documentation. but not able to understand how this function is deleting duplicates. Can you explain with this example

Anjali
New Contributor

I got my answer how this filter function deletes duplicates

Thank you for sharing the article that helped you. I took a look at it and I can definitely see the author did a great job going step-by-step, explaining how it works along the way. For anyone else looking to understand what is happening with the specific SnapLogic example, here is the explanation.

First, letโ€™s just restate the data and expression weโ€™re working with:
Data: $myarray = [โ€œFredโ€, โ€œWilmaโ€, โ€œFredโ€, โ€œBettyโ€, โ€œFredโ€, โ€œBarneyโ€]
Expression: $myarray.filter((item, pos, a) => a.indexOf(item) == pos)

At a high level, the filter() function is going to be passed three pieces of information:

  • item is the element value like โ€œFredโ€ or โ€œWilmaโ€
  • pos is the elementโ€™s position in the array starting with 0 for the first element
  • a is the array

Now letโ€™s take a look at the first use of the function and the values weโ€™re working with. Note the value of a will always be the full array.

  • item = โ€œFredโ€
  • pos = 0
  • a = [โ€œFredโ€, โ€œWilmaโ€, โ€œFredโ€, โ€œBettyโ€, โ€œFredโ€, โ€œBarneyโ€]

So on the first past we are searching the array for โ€œFredโ€ and indexOf() will return 0, which matches the value of pos, meaning it is the first occurrence. The filter() function would then move โ€œFredโ€ through to the resulting array. The second pass plays out the same situation but this time our values look like this:

  • item = โ€œWilmaโ€
  • pos = 1
  • a = [โ€œFredโ€, โ€œWilmaโ€, โ€œFredโ€, โ€œBettyโ€, โ€œFredโ€, โ€œBarneyโ€]

As indexOf() iterates through the array the first occurrence of โ€œWilmaโ€ is index 1, which matches our pos, so filter() passes โ€œWilmaโ€ through to the resulting array which would now look like [โ€œFredโ€, โ€œWilmaโ€]. On the third pass is when we observe filter() knock out a duplicate item from the array. Letโ€™s take a look at the values:

  • item = โ€œFredโ€
  • pos = 2
  • a = [โ€œFredโ€, โ€œWilmaโ€, โ€œFredโ€, โ€œBettyโ€, โ€œFredโ€, โ€œBarneyโ€]

For the third pass, the indexOf(โ€œFredโ€) result would be 0, which does not equal our pos of 2, meaning that this item has already occurred in the array, therefore filter() will not pass it through.

Hope that helps give a clearer picture of how that expression works specifically with the SnapLogic example! If youโ€™re looking for an example pipeline to test this with you can find one in our Array.filter() documentation here.