How to recursively find specific key in nested JSON?

The JSON might contain a list of objects, which needs to be searched: Python 2.7 version: def item_generator(json_input, lookup_key): if isinstance(json_input, dict): for k, v in json_input.iteritems(): if k == lookup_key: yield v else: for child_val in item_generator(v, lookup_key): yield child_val elif isinstance(json_input, list): for item in json_input: for item_val in item_generator(item, lookup_key): yield item_val … Read more

Get All Children to One List – Recursive C#

You can do SelectMany List<Location> result = myLocationList.SelectMany(x => x.Children).ToList(); You can use where condition for some selective results like List<Location> result = myLocationList.Where(y => y.ParentID == someValue) .SelectMany(x => x.Children).ToList(); If you only required Id’s of Children you can do List<long> idResult = myLocationList.SelectMany(x => x.Children) .SelectMany(x => x.ID).ToList();

Swift: Recursively cycle through all subviews to find a specific class and append to an array

Based on Aaron Brager and ullstrm answers Details Xcode 9.1, Swift 4, Xcode Version 10.3 (10G8), Swift 5 Solution extension UIView { class func getAllSubviews<T: UIView>(from parenView: UIView) -> [T] { return parenView.subviews.flatMap { subView -> [T] in var result = getAllSubviews(from: subView) as [T] if let view = subView as? T { result.append(view) } … Read more

Deleting non-empty directories in Java

If you really want your recursive directory deletion to follow through symbolic links, then I don’t think there is any platform independent way of doing so without keeping track of all the directories you have traversed. However, in pretty much every case I can think of you would just want to delete the actual symbolic … Read more

Recursion: how to avoid Python set changed set during iteration RuntimeError

I think the problem is here: for color in self.domains[var]: if not self._valid(var, color): continue self.map[var].color = color for key in node.neighbors: if color in self.domains[key]: self.domains[key].remove(color) # This is potentially bad. if key == var when self.domains[key].remove(color) is called, you change the size of the set you’re currently iterating over. You can avoid this … Read more

Javascript – deepEqual Comparison

As you suspect, you’re returning the match of the first property seen. You should return false if that property doesn’t match, but keep looking otherwise. Also, return false if there’s no prop property found on y (that is, the counts match, but not the actual properties). If all properties have matched, return true: var deepEqual … Read more