小编aka*_*koz的帖子

从特定元素添加的数组中的Swift sum数

假设我在下面的数组中有一些数字

let numberArray = [1, 3, 4, 6, 9, 14, 16]
Run Code Online (Sandbox Code Playgroud)

我知道如何使用reduce方法对它们进行总结,但是如何从特定元素开始添加数字.例如:

//Sum all up by reduce
let sumAll = numberArray.reduce(0, +)
//sumAll = 53 

//I want to start counting from the fourth element in the array which is 6
//so the total should be 45. [6 + 9 + 14 + 16]
Run Code Online (Sandbox Code Playgroud)

我应该用什么方法来实现这个目标?
提前致谢.

arrays sum swift

1
推荐指数
1
解决办法
447
查看次数

标签 统计

arrays ×1

sum ×1

swift ×1