Search code examples
jsonnet

Add elements of array in jsonnet


I am trying to add elements of an array in jsonnet. Can someone post a sample?

Sample input: [0, 1, 2, 3]

output 6


Solution

  • You can use std.foldl() as the "aggregation" function:

    local myArray = [0, 1, 2, 3];
    
    std.foldl(function(x, y) (x + y), myArray, 0)