从数组中删除一个嵌套的NSArray(Objective c)

时间:2017-10-19 16:55:34

标签: objective-c nsarray

我在myArray中有这种模式:

myArray[0][0] = "28.04"
myArray[1][0] = "21.12"
myArray[2][0] = "12.33"

我想要这样:

myArray[0] = "28.04"
myArray[1] = "21.12"
myArray[2] = "12.33"

1 个答案:

答案 0 :(得分:-1)

您需要一个名为flatmap的高阶函数

- (NSArray *)flatMap:(id (^)(id obj))block {
    NSMutableArray *mutableArray = [NSMutableArray new];
    [self enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        id _obj = block(obj);
        if ([_obj isKindOfClass:[NSArray class]]) {
            NSArray *_array = [_obj flatMap:block];
            [mutableArray addObjectsFromArray:_array];
            return;
        }
        [mutableArray addObject:_obj];
    }];
    return mutableArray;
}