In this post:
The Problem
Ok so here's what was happening to me and I didn't know why.
I was doing a bunch of transformations and at one point I'd add some keys and/or values into an array. So, some of the arrays were of type Array<String> (because no key was added here) while some of them where Array<Key | String>
I didn't think much about it at first, since I would -incorrectly- see all the arrays in the JSON output were of type Array<String>. Like this:
ℹ️ Note The item in line 4 was coerced into a Key for demonstration purposes.
So imagine my surprise when I tried to use a distinctBy function with the above arrays and I'd still get the same two arrays in the output.
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!
It was until one of those million times finally led me to see the array types were actually different 🤦
Possible Solutions
Solution 1
There are different solutions for this problem. One would be to make sure any new items are of type String and not Key. This completely removes the issue since now you'd be comparing two arrays of type Array<String>, which correctly returns only one array in the output.
Solution 2
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 function itself. Like this:
Since we're using the 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.
I hope this post brings you more DataWeave knowledge or helps you fix some code :D
Let me know your thoughts in the comments 👇
Subscribe to receive notifications as soon as new content is published ✨
💬 Prost! 🍻
StudyProfy has been an incredible resource for me and many other college students who need help with their essays. Their professional writers deliver well-structured and thoroughly researched papers that meet academic standards. When I was struggling with my application, their college admission essay writers helped me craft a compelling and polished essay that truly represented my strengths. The service is reliable, efficient, and makes the entire writing process stress-free. If you need academic assistance, StudyProfy is the perfect solution to help you achieve your academic goals with confidence. I highly recommend their services!