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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • Unlocking Game Development: A Review of ‘Learning C# By Developing Games With Unity'
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • What to Pay Attention to as Automation Upends the Developer Experience
  • Does the OCP Exam Still Make Sense?

Trending

  • Unlocking Game Development: A Review of ‘Learning C# By Developing Games With Unity'
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • What to Pay Attention to as Automation Upends the Developer Experience
  • Does the OCP Exam Still Make Sense?
  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.

Lalit Panwar user avatar by
Lalit Panwar
·
Aug. 08, 19 · Tutorial
Like (3)
Save
Tweet
Share
25.55K Views

Join the DZone community and get the full member experience.

Join For Free

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.

Trending

  • Unlocking Game Development: A Review of ‘Learning C# By Developing Games With Unity'
  • Mastering Time Series Analysis: Techniques, Models, and Strategies
  • What to Pay Attention to as Automation Upends the Developer Experience
  • Does the OCP Exam Still Make Sense?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: