DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Reversing an Array: An Exploration of Array Manipulation
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window
  • Binary Search in Python

Trending

  • How You Clear Your HTML5 Canvas Matters
  • ScyllaDB in Enterprise Java: High-Performance NoSQL With Eclipse JNoSQL
  • Replay, Scheduling, and Throttling: Engineering a Self-Service Kafka Streaming Tool at Scale
  • Taming Parameter Sniffing in SQL Server 2022 With PSP Optimization

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Data Engineering
  3. Data
  4. Power of Arrays Module in DataWeave

Power of Arrays Module in DataWeave

Let's look at the power of the arrays module in DataWeave.

By 
Lalit Panwar
·
Aug. 08, 19 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
26.9K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

We are well aware that DataWeave plays an important role in developing integration applications using MuleSoft. It also contains a lot of modules that enhance its power. Out of all of these modules, we have the Arrays module (dw::core::Arrays), which helps us utilize arrays in our integration application efficiently.

This article will discuss the same Arrays module. Let’s start with the list of available functions.

countBy

indexWhere

some

divideBy

join

splitAt

drop

leftJoin

splitWhere

dropWhile

outerJoin

sumBy

every

partition

take

indexOf

slice

takeWhile

Let us now go into the details of each function.

  1. countBy

With the help of this function, we can find out the number of elements in an array as per a Boolean expression, it will look for the elements in a list (array) that match the results of the function.

Parameters

  • Name of Array
  • General Expression or Lambda Expression returning a Boolean value.

Syntax

Image title

Example

Image title

Output

Image title

  1. divideBy

The divideBy function can be used to break up a list (array) into sub-lists (sub-arrays) that contain the specified number of elements. When there are fewer elements in the input array than the specified number, the function fills the sub-array with those elements. When there are more elements, the function fills as many sub-arrays needed with the extra elements.

Parameters

  • Name of Array
  • Number of elements in the sub-lists

Example

Image title

Output

Image title

  1. drop

Drop function will drop first n elements. It returns the original array when n <= 0 and an empty array when n > sizeOf(array).

Parameters:

  • Array Name
  • Number of elements to drop

Example

Image title

Output

Image title

  1. dropWhile

Similar to the previous drop function but it will drop as per a Boolean expression.

Parameters

  • Array Name
  • Boolean Expression

Example

Image title

Output

Image title

  1. every

Returns true if every element in the array matches the boolean expression. The function stops iterating after the first negative evaluation of an element in the array.

Parameters

  • Array Name
  • Boolean Expression

Example

Image title

Output

Image title

  1. indexOf

indexOf function returns the index of the first occurrence of an element within the array.

Parameters

  • Array Name
  • Any Value

Example

Image title

Output

Image title

  1. indexWhere

indexWhere can be used to return the index of the first occurrence of an element that matches a condition within the array.

Parameters

  • Array Name
  • Boolean Expression

Example

Image title

Output

Image title

  1. join

Joins two Arrays of Objects by an ID criterion. It will return an Array of Objects containing only the objects for which the ID from the left group can be matched with an ID from the right group.

Parameters

  • Left Array of Objects
  • Right Array of Objects
  • Left Criteria to extract the key column for the left collection.
  • Right Criteria to extract the key column for the right collection.

Example

Image title

Output

Image title

  1. leftJoin

Joins two Arrays of Objects by a given ID criterion. It will return an Array of all the left items merged by ID with the right items if any exists.

Parameters

  • Left Array of Objects
  • Right Array of Objects
  • Left Criteria to extract the key column for the left collection.
  • Right Criteria to extract the key column for the right collection.

Example

Image title

Output

Image title

  1. outerJoin

Joins two Arrays of Objects by a given ID criterion. It will return an Array with all the left items merged by ID with the left items if any exists, and the right items that are not present in the left.

Parameters

  • Left Array of Objects
  • Right Array of Objects
  • Left Criteria to extract the key column for the left collection.
  • Right Criteria to extract the key column for the right collection.

Example

Image title

Output

Image title

  1. partition

Separates the array into the elements that satisfy the condition and those that don't.

Parameters

  • Array Name.
  • Boolean/Lambda Expression returning a boolean value.

Example

Image title

Output

Image title

  1. slice

Selects the interval of elements that satisfy the condition: from <= indexOf(array) < until

Parameters

  • An Array
  • Starting Index of the slice
  • End Index of the slice

Example

Image title

Output

Image title

  1. some

Returns true if an element in the array matches the specified condition. The function stops iterating after the first match to an element in the array.

Parameters

  • The input array.
  • A condition (or expression) used to match elements in the array.

Example

Image title

Output

Image title

  1. splitAt

Splits an array into two at a given position.

Parameters

  • The Array of elements.
  • The index to split at.

Example

Image title

Output

Image title

  1. splitWhere

Splits an array into two at the first position where the condition is met.

Parameters

  • The Array of elements to split.
  • The condition (or expression) used to match an element in the array.

Example

Image title

Output

Image title

  1. sumBy

Returns the sum of the values of the elements in an array.

Parameters

  • The input array.
  • A DataWeave selector that selects the values of the numbers in the input array.

Example

Image title

Output

Image title

  1. take

Selects first n elements. It returns an empty array when n <= 0 and the original array when n > sizeOf(array).

Parameters

  • The left Array of elements.
  • The number of elements to take.

Example

Image title

Output

Image title

  1. takeWhile

Takes elements for the array while the condition is met.

Parameters

  • The Array of elements.
  • The condition (or expression) used to match an element in the array.

Example

Image title

Output

Image title

Thanks for reading!

Data structure Element

Published at DZone with permission of Lalit Panwar. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Reversing an Array: An Exploration of Array Manipulation
  • Unlocking the Potential of Binary Search Trees with C# Programming
  • Sliding Window
  • Binary Search in Python

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook