Reduce array to a single value


I have an array and want to recursively perform a specific action on all elements and get one single value/result.


// Initialize the Array
var a = [1,2,3,4,5,6]

// Recursively perform the defined action ($0 + $1)
// to all elements and get result

var sum = a.reduce(0) { $0 + $1 }


Find answers here

Hi there! Ask Us a Question We are ready to help