0
嵌套意味着採用一組鍵值對並按指定鍵分層分組。請參閱此頁面中的示例:http://bl.ocks.org/d/3176159/。如果不是的話,我只會嘗試移除https://github.com/mbostock/d3/blob/master/src/core/nest.js,但我不想重新發明輪子。Objective-C中是否有類似D3的Nest功能?
嵌套意味着採用一組鍵值對並按指定鍵分層分組。請參閱此頁面中的示例:http://bl.ocks.org/d/3176159/。如果不是的話,我只會嘗試移除https://github.com/mbostock/d3/blob/master/src/core/nest.js,但我不想重新發明輪子。Objective-C中是否有類似D3的Nest功能?
這是我想出來的答案。如果您有改進建議,請告訴我。
// Wrapper method
// keys are in order of hierarchy
- (NSMutableArray *)nestArray:(NSArray *)array withKeys:(NSArray *)keys
{
return [self nestArray:array withKeys:keys depth:0];
}
// Private
// Assumes arrays of dictionaries with strings as the entries.
- (NSMutableArray *)nestArray:(NSArray *)array withKeys:(NSArray *)keys depth:(int)depth
{
// Current key
NSString *key = [keys objectAtIndex:depth];
depth++;
// Create dictionary of the keys
NSMutableDictionary *map = [[NSMutableDictionary alloc] init];
for (NSDictionary *dictionary in array) {
NSString *value = [dictionary objectForKey:key];
if ([map objectForKey:value]) {
[[map objectForKey:value] addObject:dictionary];
} else {
[map setObject:[NSMutableArray arrayWithObject:dictionary] forKey:value];
}
}
NSMutableArray *nest = [[NSMutableArray alloc] init];
for (NSString *valkey in [map allKeys]) {
NSMutableArray *values = [map objectForKey:valkey];
if (depth < keys.count) {
values = [self nestArray:[NSArray arrayWithArray:array] withKeys:keys depth:depth];
}
NSMutableDictionary *dictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:valkey,@"key",values,@"values", nil];
[nest addObject:dictionary];
}
return nest;
}
如果我們沒有必須去通過代碼試圖找出它做什麼,因爲你**告訴它我們這將是有益的。** – 2013-02-14 06:08:18
好一點!我添加了一些解釋和例子的鏈接。 – 2013-02-14 06:11:27
謝謝!那麼,爲了記錄,我不知道這一切立即做到。但是,您可以很容易地重新實現它。 – 2013-02-14 06:21:49