9

Working of map and mapObject function in MuleSoft

 2 years ago
source link: https://blog.knoldus.com/working-of-map-and-mapobject-function-in-mulesoft/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Working of map and mapObject function in MuleSoft

Reading Time: 2 minutes

Before understanding the map and mapObject functions in MulseSoft, let’s get familiar with the term MuleSoft. What is Mulesoft and what is Mulesoft used for?

What is MuleSoft?

MuleSoft provides an integration platform called Anypoint Platform to help businesses connect data, applications and devices across on-premises and cloud computing environments.

What is MuleSoft used for?

MuleSoft used to connect a variety of data sources, applications, perform analytics and ETL processes.

map function

  • It transforms every item in an Array and returns an Array.
  • map takes two parameters an Array and a lambda.
  • The lambda takes in two arguments
map(Array<T>, ((T, Number) -> R)): Array<R>

Here are two type of definition, T and R.

  • T represents the type of items that the input Array contains.
  • R represents the type of items that output Array returns.
  • Number represents the index of the input Array.

Input

[1,2,3,4,5]

DataWeave Script

%dw 2.0
%dw 2.0
output json
---
payload map (n, idx) -> {
    "value": n+1,
    "index": idx

}

Output

[
  {
    "value": 2,
    "index": 0
  },
  {
    "value": 3,
    "index": 1
  },
  {
    "value": 4,
    "index": 2
  },
  {
    "value": 5,
    "index": 3
  },
  {
    "value": 6,
    "index": 4
  }
]

mapObject function

  • It transforms an Object to a new Object.
  • mapObject is used when we want to change the keys and/or values on an Object to be something else.

Syntax

mapObject(Object<K,V>, (V,K,Number) -> Object): Object

mapObject takes two parameter an Object and a lambda.

Object takes two parametes a value and key, and returns a new Object

mapObject Object<K,V> : Object

lambda takes three parametes a value, key and index, and returns a new Object.

mapObject (V,K,Number) -> Object

Here are two type definition, V and K.

  • V represents the value of the Object.
  • K represents the key of the Object.
  • Number represents the index of the Object.

Input

[
  {"First Name": "Max", "Last Name": "The Mule"},
  {"First Name": "Albert", "Last Name": "Einstein"}
]

DataWeave Script

%dw 2.0
output json
---
payload map ((item, index) -> 
        item mapObject(v,k,n)-> {
    (lower(k)) : v
    }
)

Output

[
  {
    "first name": "Max",
    "last name": "The Mule"
  },
  {
    "first name": "Albert",
    "last name": "Einstein"
  }
]
That’s all for this blog. I hope you understood the concept. .However If you have any doubt, feel free to contact me at [email protected].

References

https://docs.mulesoft.com/mule-runtime/4.3/dw-core-functions-map

https://docs.mulesoft.com/mule-runtime/4.3/dw-core-functions-mapobject


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK