coderz.py

Keep Coding Keep Cheering!

Arrays (dw::core::Arrays)

Arrays (dw::core::Arrays)

This module contains helper functions for working with arrays. In this tutorial will cover the most common DataWeave functions you will need to work with Arrays.

DataWeave 2.0 makes it easier to define an array with a range (“<start-index> to <end-index>”). The above expression will set the value from 1 to 15 ([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) as an array for variable named “range”.

  • We will start with a brief review of filter, used to remove items from an Array based on some criteria.
  • We’ll then discuss map, which is used to transform every item in an Array into something else.
  • We’ll follow up with distinctBy, and groupBy, which can be used to eliminate duplicates in an Array and group like-kind items together, respectively.
  • We’ll finish with reduce. reduce is unlike the other functions because of how general-purpose it is.

Functions

NameDescription
countByCounts the elements in an array that return true when the matching function is applied to the value of each element.
divideByBreaks up an array into sub-arrays that contain the specified number of elements.
dropDrops the first n elements. It returns the original array when n <= 0 and an empty array when n > sizeOf(array).
dropWhileDrops elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition.
everyReturns true if every element in the array matches the condition.
firstWithReturns the first element that satisfies the condition, or returns null if no element meets the condition.
indexOfReturns the index of the first occurrence of an element within the array. If the value is not found, the function returns -1.
indexWhereReturns the index of the first occurrence of an element that matches a condition within the array. If no element matches the condition, the function returns -1.
joinJoins two arrays of objects by a given ID criteria.
leftJoinJoins two arrays of objects by a given ID criteria.
outerJoinJoins two array of objects by a given ID criteria.
partitionSeparates the array into the elements that satisfy the condition from those that do not.
sliceSelects the interval of elements that satisfy the condition: from <= indexOf(array) < until
someReturns true if at least one element in the array matches the specified condition.
splitAtSplits an array into two at a given position.
splitWhereSplits an array into two at the first position where the condition is met.
sumByReturns the sum of the values of the elements in an array.
takeSelects the first n elements. It returns an empty array when n <= 0 and the original array when n > sizeOf(array).
takeWhileSelects elements from the array while the condition is met but stops the selection process when it reaches an element that fails to satisfy the condition.

Follow Me

If you like my post please follow me to read my latest post on programming and technology.

Instagram

Facebook

Leave a Comment

Your email address will not be published. Required fields are marked *

Advertisement