Search code examples
muledataweavemulesoft

Get sum of the values of objects in an array


I have a dataweave input as given below.

%dw 2.0
output application/json
var demo = [
    {
        "mat" : 1000,
        "seg" : "z"
    },
    {
        "mat" : 2000,
        "seg" : "b"
    },
    {
        "mat" : 3000,
        "seg" : "x"
    }
]
var count = 0
---
count 

I need the sum of the "mat" field, the count should 6000. How do I achieve this ?


Solution

  • I did it like this

    %dw 2.0
    output application/json
    var demo = [
        {
            "mat" : 1000,
            "seg" : "z"
        },
        {
            "mat" : 2000,
            "seg" : "b"
        },
        {
            "mat" : 3000,
            "seg" : "x"
        }
    ]
    var count = sum(demo.mat)
    ---
    count