0

I have a SwiftyJSON array that is nested several levels and I need to filter the array based on a value in the lowest level. Below is an example of an array. I need to filter it on Active == true. What is the most efficient way to accomplish this?

var colors = JSON([
"Apple" : [
        "Yellow" : [
            "Active" : true,
            "Working" : true,
            "Value" : 0
        ],
        "Red" : [
            "Active" : false,
            "Working" : true,
            "Value" : 0
        ]
],
"Banana" : [
        "Blue" : [
            "Active" : false,
            "Working" : true,
            "Value" : 0
        ],
        "Green" : [
            "Active" : true,
            "Working" : true,
            "Value" : 0
        ]
]
])

Desired output:

"Apple" : [
        "Yellow" : [
            "Active" : true,
            "Working" : true,
            "Value" : 0
        ]
],
"Banana" : [
        "Green" : [
            "Active" : true,
            "Working" : true,
            "Value" : 0
        ]
]
2
  • 1
    The array is a dictionary. Commented Oct 11, 2016 at 20:23
  • @MikeDeluca Check my answer once. Commented Oct 11, 2016 at 22:00

3 Answers 3

1

Check this out

var filteredArray = [JSON]()

for item in colors {
    for subItem in item.1 {
        if subItem.1["Active"].boolValue {
           filteredArray.append(JSON([item.0:JSON([subItem.0:subItem.1])]))
        }
    }
}

print(filteredArray)

Output is the array of Dictionaries of filtered sub dictionaries with Active true:

[{
  "Apple" : {
    "Yellow" : {
      "Working" : true,
      "Value" : 0,
      "Active" : true
    }
  }
}, {
  "Banana" : {
    "Green" : {
      "Working" : true,
      "Value" : 0,
      "Active" : true
    }
  }
}]
Sign up to request clarification or add additional context in comments.

Comments

0

you can create a loop that will read all colors and within this loop to create one that will read the other items in the second array. Within the second is you check if the item is valid or not and save it in another array

let activeItems = []

for item in colors {
   for i in item as NSDictionary {
      if i.objectForKey("Active") == true {
         activeItems.addObject(i)
      }
   }
}

3 Comments

This doesn't work because the array is JSON, giving error cannot convert value of type (String, JSON) to type NSDictionary
It is not an array.
@MikeDeluca No need to convert it to NSDictionary, it can be solved by simple [JSON] array of SwiftyJSON. Check my answer
0
        var result = [String:[String:JSON]]()
        for (key, json) in colors.dictionaryValue {
            let filtered = json.dictionaryValue.filter{$0.1["Active"].boolValue}
            guard filtered.count > 0 else { continue }

            var subDic = [String:JSON]()
            filtered.forEach{subDic[$0.0] = $0.1}
            result[key] = subDic
        }
        print(result)

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.