โ06-23-2021 02:15 PM
Can anyone make me understand what the below function is doing?
filter((a, b, c) => c.indexOf(a) == b)
โ06-23-2021 06:47 PM
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.
โ06-24-2021 01:22 AM
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
โ06-24-2021 01:45 AM
I got my answer how this filter function deletes duplicates
โ06-25-2021 07:16 AM
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 elementa
is the arrayNow 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
= 0a
= [โ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
= 1a
= [โ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
= 2a
= [โ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.