Manipulation On Array In Mule 4

 

In this tutorial we will demonstrate how can we do Manipulation On Array In Mule 4. To use this module, we first need to import DataWeave code, for example, by adding the line import * from dw::core::Arrays to the header of your DataWeave script

Please find below few important Array function which will be useful while working on Array In Dataweave

Note: This Module can be used in Dataweave 2.2 and later

 

Drop Function

 

This function is introduced is Dataweave 2.2.0

This function drops the elements from array and return the remaining elements as per the condition.

 

%dw 2.0
import * from dw::core::Arrays
var samplearray = ["124", "35454", "Sachin","Amit"]
output application/json
---

drop(samplearray, 2)

Result:

[
    "Sachin",
    "Amit"
]

 

IndexOf Function

 

It Returns the index of the first occurrence of an element within the array. If the value is not found it will return -1

 

Dataweave Expression:

%dw 2.0
import * from dw::core::Arrays
var samplearray = ["124", "35454", "Sachin","Amit"]
output application/json
---

indexOf(samplearray, "Sachin")

Result:

2

 

takeWhile Function

 

Selects 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

 

Dataweave Expression:

%dw 2.0
import * from dw::core::Arrays
output application/json
var arr = [1,2,3,4,1,2]
---

arr takeWhile $ <= 2

Result:

[
    1,
    2
]

 

sumBy Function

 

It returns the sum of the values inside array

Input:

[ { items: 233}, { items: 324 }, { items: 30 } ]

 

Dataweave Expression:

%dw 2.0
import * from dw::core::Arrays
output application/json

---
{
  "TotalItems" : [ 
    { items: 233}, 
    { items: 324 }, 
    { items: 30 }
  ] sumBy $.items
}

Result:

{
  "TotalItems": 587
}

 

Join 2 Arrays With Matching Id Using Join Function

 

In this part we will create a dataweave expression which will join 2 Different Array on ths basis of the ID

Input:

Items Array

[{id: "12", name:"Tea"},{id: "22", name:"Salt"},{id: "3", name:"Soap"},{id: "5", name:"Butter"}]

Price Array

[{Id: "12", price:123},{Id: "3", price:24}, {Id: "22", price:20}, {Id: "4", price:456}]

 

Dataweave Expression:

%dw 2.0
import * from dw::core::Arrays

var items = [{id: "12", name:"Tea"},{id: "22", name:"Salt"},{id: "3", name:"Soap"},{id: "5", name:"Butter"}]

var Prices = [{Id: "12", price:123},{Id: "3", price:24}, {Id: "22", price:20}, {Id: "4", price:456}]

output application/json
---
join(items, Prices, (item) -> item.id, (price) -> price.Id)

 

Result:

[
  {
    "l": {
      "id": "12",
      "name": "Tea"
    },
    "r": {
      "Id": "12",
      "price": 123
    }
  },
  {
    "l": {
      "id": "22",
      "name": "Salt"
    },
    "r": {
      "Id": "22",
      "price": 20
    }
  },
  {
    "l": {
      "id": "3",
      "name": "Soap"
    },
    "r": {
      "Id": "3",
      "price": 24
    }
  }
]
  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anu
Anu
3 years ago

how to convert Array to Object?