更新时间:2023-02-19 11:34:40
更新:
现在有使用 preFIX
来得到一个数组的第n个元素的可能性。检查 @ mluisbrown的回答一个解释如何使用preFIX。
Update:
There is now the possibility to use prefix
to get the first n elements of an array. Check @mluisbrown's answer for an explanation how to use prefix.
原来的答复:
你可以这样做很容易,而不过滤器
,地图
或减少
通过只返回一个范围的数组:
Original Answer:
You can do it really easy without filter
, map
or reduce
by just returning a range of your array:
var wholeArray = [1, 2, 3, 4, 5, 6]
var n = 5
var firstFive = wholeArray[0..<n] // 1,2,3,4,5