Translate

Thursday 2 May 2024

What’s the output from mapobject function ? in MuleSoft 363

 What’s the output from mapobject function ? in MuleSoft


The output of the mapObject function in MuleSoft 4 is a new object with the same keys as the input object, but with the values transformed based on the provided function.

Here's a breakdown of how it works:

Function Syntax:




mapObject(object, transformationFunction)

Parameters:

  • object: The input object containing key-value pairs.

  • transformationFunction: A function that takes three arguments:

  • key: The key (name) of the current element in the object (String type).

  • value: The value of the current element in the object (can be any type).

  • index (optional): The index position of the current element within the object (Number type, available in MuleSoft 4.3 and above).

Function Behavior:

  1. The mapObject function iterates over each key-value pair in the input object.

  2. For each iteration, it calls the provided transformationFunction with the current key, value, and optionally the index (if applicable).

  3. The transformationFunction you define performs the desired transformation on the value.

  4. The mapObject function collects the transformed value returned by the function and associates it with the original key in a new object.

  5. Once all iterations are complete, the mapObject function returns the newly created object with the transformed values.

Example:




%dw 2.0
input: { name: "John", age: 30 }
output: mapObject(input, (key, value) =>
  if (key == "age") then value * 2 else value end)

// Output: { name: "John", age: 60 } (age is doubled)

Key Points:

  • The mapObject function doesn't modify the original object. It creates a new object with the transformed values.

  • You can use various expressions and functions within the transformationFunction to achieve different types of value manipulation.

  • The optional index argument provides flexibility for scenarios where you need to perform transformations based on the element's position within the object (available in MuleSoft 4.3 and above).

By understanding the output and behavior of mapObject, you can effectively manipulate objects and transform their values within your MuleSoft 4 integration flows using DataWeave.


No comments:

Post a Comment

Note: only a member of this blog may post a comment.