You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/blog/array-string-key-dataweave.mdx
+12Lines changed: 12 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -23,10 +23,16 @@ I didn't think much about it at first, since I would -incorrectly- see all the a
23
23
24
24

25
25
26
+
> [!PLAYGROUND]
27
+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript1)
28
+
26
29
So imagine my surprise when I tried to use a [distinctBy](https://docs.mulesoft.com/dataweave/latest/dw-core-functions-distinctby) function with the above arrays and I'd still get the same two arrays in the output.
27
30
28
31

29
32
33
+
> [!PLAYGROUND]
34
+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript2)
35
+
30
36
I started losing my mind trying to troubleshoot the problem a million different ways because I didn't notice the items in the arrays were of different types!
31
37
32
38
It was until one of those million times finally led me to see the array types were actually different 🤦
@@ -39,12 +45,18 @@ There are different solutions for this problem. One would be to make sure any ne
39
45
40
46

41
47
48
+
> [!PLAYGROUND]
49
+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript3)
50
+
42
51
### Solution 2
43
52
44
53
If you can't change the type of the items or it would take too many iterations to do, another thing you can do is to transform the items inside the [distinctBy](https://docs.mulesoft.com/dataweave/latest/dw-core-functions-distinctby) function itself. Like this:
45
54
46
55

47
56
57
+
> [!PLAYGROUND]
58
+
> [Open in Playground](https://dataweave.mulesoft.com/learn/playground?projectMethod=GHRepo&repo=ProstDev%2Fdataweave-playground-previews&path=scripts%2Farray-string-key%2Fscript4)
59
+
48
60
Since we're using the [joinBy](https://docs.mulesoft.com/dataweave/latest/dw-core-functions-joinby) function, we're transforming the values of arr1 and arr2 into concatenated strings and THEN comparing them. Both arr1 and arr2 would be "key,string", which makes these values the same.
49
61
50
62
I hope this post brings you more DataWeave knowledge or helps you fix some code :D
0 commit comments