Split large Array in Array of two elements

前端 未结 7 2100
借酒劲吻你
借酒劲吻你 2020-12-20 17:53

I have large list of objects and I need to split them in a group of two elements for UI propouse.

Example:

[0, 1, 2, 3, 4, 5, 6]

Becomes

7条回答
  •  孤城傲影
    2020-12-20 18:07

    You can use oisdk's awesome SwiftSequence framework. There is the chunk function which does exactly what you want:

    [1, 2, 3, 4, 5].chunk(2)
    
    [[1, 2], [3, 4], [5]]
    

    Also there's a LOT more functions for sequences, you should definitely check it out.

    You can have a look at his implementation of chunk here (It uses generators)

提交回复
热议问题