Reputation:
I have an 2d array like this:
let array= [ [0,2], [4,5], [4,1], [0,4] ]
and I'd like to get ONE element thats 0.
index is the smallest in the array.
Result should be this:
let result = [0,2] // not also [0,4] because just the first one
This is what I've tried so far but obviously this is not a working solution :p
let result = array.filter { $0[1].map { return $0.min() } }
Thanks in advance, jonas
Upvotes: 0
Views: 320
Reputation: 54745
You can simply use Array.min
then compare the first element of the nested arrays. min
will return the first element in the original array that fits the minimum function in the closure in case there were several minima.
let array = [ [0,2], [4,5], [4,1], [0,4] ]
let result = array.min(by: {$0.first! < $1.first!}) //[0,2]
Upvotes: 1