1

I have the following structure

aSectionArray = [[objA, objB, objC], [obj1, obj2, obj3], [objX, objY, objZ]]

objA is having a bool, say isEnabled.

I need to filter the aSectionArray based this bool (say isEnabled == true).

Help needed.

3
  • 2
    What do you want to achieve through the filter? Do you want all objects where isEnabled is true? Commented Jul 20, 2016 at 19:13
  • 1
    What's the array's type? Commented Jul 20, 2016 at 19:25
  • 1
    Not clear if you're wanting to flatten and filter as Eric suggests, or return only the elements of the outer array that have a matching element, or an array of arrays of matching objects, or... Commented Jul 20, 2016 at 20:20

1 Answer 1

6

You could use flatten() and filter, like this (Swift 2):

let result = aSectionArray.flatten().filter { $0.isEnabled }

it will give you the objects where isEnabled is true.

We use flatten() to make the 2D array into a 1D array, and we use filter to get the objects where the closure verifies.

In Swift 3 (Xcode 8 beta 6) flatten has become joined:

let result = aSectionArray.joined().filter { $0.isEnabled }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.