处理数组对象中,筛选出相同的元素对象。重新整合新数组

下面有个数据接口,后端把所有数据都返回到一个数组中,为了实现这种类似美团的结构,只能自己进行组装拼接。

1.结构类型如下

[
{
"parentId": "0",
"frontName": "美食",
"frontDesc": "美食",
"id": "1073448473380732930",
"catClass": 1,
"wapBannerUrl": "",
"version": "5",
"showIndex": 1,
"name": "美食",
"state": 1
},
{
"parentId": "0",
"frontName": "住宿",
"frontDesc": "住宿",
"id": "1073449379929210881",
"catClass": 1,
"wapBannerUrl": "",
"version": "1",
"showIndex": 2,
"name": "酒店住宿",
"state": 1
},
{
"parentId": "0",
"frontName": "休闲娱乐",
"frontDesc": "休闲娱乐",
"id": "1073449479862697985",
"catClass": 1,
"wapBannerUrl": "",
"version": "0",
"showIndex": 3,
"name": "休闲娱乐",
"state": 1
},
{
"parentId": "0",
"frontName": "丽人丽发",
"frontDesc": "丽人丽发",
"id": "1073449566043062273",
"catClass": 1,
"wapBannerUrl": "",
"version": "1",
"showIndex": 4,
"name": "丽人丽发",
"state": 1
},
{
"parentId": "0",
"frontName": "按摩足疗",
"frontDesc": "按摩足疗",
"id": "1073450398151032834",
"catClass": 1,
"wapBannerUrl": "",
"version": "1",
"showIndex": 5,
"name": "按摩足疗",
"state": 1
},
{
"parentId": "0",
"frontName": "旅游",
"frontDesc": "旅游",
"id": "1073451397716590594",
"catClass": 1,
"wapBannerUrl": "",
"version": "0",
"showIndex": 6,
"name": "旅游",
"state": 1
},
{
"parentId": "0",
"frontName": "生活服务",
"frontDesc": "生活服务",
"id": "1073451484584820737",
"catClass": 1,
"wapBannerUrl": "",
"version": "1",
"showIndex": 7,
"name": "生活服务",
"state": 1
},
{
"parentId": "0",
"frontName": "百货商城",
"frontDesc": "百货商城",
"id": "1073451567887892481",
"catClass": 1,
"wapBannerUrl": "",
"version": "1",
"showIndex": 8,
"name": "百货商城",
"state": 1
},
{
"parentId": "0",
"frontName": "母婴亲子",
"frontDesc": "母婴亲子",
"id": "1073451811165913090",
"catClass": 1,
"wapBannerUrl": "",
"version": "1",
"showIndex": 9,
"name": "母婴亲子",
"state": 1
},
{
"parentId": "1073448473380732930",
"frontName": "火锅",
"frontDesc": "火锅",
"id": "1078211194399748097",
"catClass": 2,
"wapBannerUrl": "",
"version": "0",
"showIndex": 1,
"name": "火锅",
"state": 1
},
{
"parentId": "1073449379929210881",
"frontName": "星级酒店",
"frontDesc": "星级酒店",
"id": "1078214962575044609",
"catClass": 2,
"wapBannerUrl": "",
"version": "0",
"showIndex": 1,
"name": "星级酒店",
"state": 1
},
{
"parentId": "1073448473380732930",
"frontName": "炒菜",
"frontDesc": "炒菜",
"id": "1078215031164497921",
"catClass": 2,
"wapBannerUrl": "",
"version": "0",
"showIndex": 1,
"name": "炒菜",
"state": 1
},
{
"parentId": "1073449479862697985",
"frontName": "KTV",
"frontDesc": "KTV",
"id": "1078215132553408513",
"catClass": 2,
"wapBannerUrl": "",
"version": "0",
"showIndex": 1,
"name": "KTV",
"state": 1
}
]

UI页面如图所示

2342397-3bfa574d431b6e0f.jpg
1271547994388_.pic.jpg

思路:

  1. 先找到一级数组 对象
    2.再通过判断每一个对象父parentId 和 一级数组对象id相等 ,筛选出相同的二级对象数组,然后放到对应的父对象中。重新组装成一个新的数组。

代码如下:

  NSMutableArray *array = [NSMutableArray arrayWithArray:arr];
    NSMutableArray *dateMutablearray = [NSMutableArray array];
    NSMutableArray *firstArray = [NSMutableArray array];

    for (int i = 0; i < array.count; i ++) {
        NSDictionary *dic1 =array[i];

        NSString *string = array[i][@"id"];
        
        if ([dic1[@"parentId"] isEqualToString:@"0"]) {

            [firstArray addObject:dic1];
        }
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        NSMutableDictionary *dicParm = [NSMutableDictionary dictionary];
        [dicParm addEntriesFromDictionary:array[i]];
        
        for (int j = 0; j < array.count; j ++) {
            NSDictionary *dic2 =array[j];
            NSString *jstring = array[j][@"parentId"];

            if([string isEqualToString:jstring]){
                [tempArray addObject:dic2];
                [dicParm setValue:tempArray forKey:@"shopCategoryList"];
            }
        }
        
        [dateMutablearray addObject:dicParm];
        
        NSLog(@"dateMutablearray ===%@ %@  ---dicParm%@",dateMutablearray,tempArray,dicParm);

    }
    
 NSArray *cateArr =  [dateMutablearray subarrayWithRange:NSMakeRange(0, firstArray.count)];

结果如下

<__NSArrayI 0x2831b7c00>(
{
parentId = 0;
frontName = 美食;
frontDesc = 美食;
id = 1073448473380732930;
catClass = 1;
shopCategoryList = (
{
1073448473380732930,
火锅,
火锅,
1078211194399748097,
2,
,
0,
1,
火锅,
1,
}
,
{
1073448473380732930,
自助餐,
自助餐,
1086978788442603521,
2,
,
1,
2,
自助餐,
1,
}
,
{
1073448473380732930,
江浙菜,
江浙菜,
1086980586226155521,
2,
,
0,
3,
江浙菜,
1,
}
,
)
;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/d89c495c-8edd-4a1b-83f2-9d0ca4e7f31b.png;
version = 5;
showIndex = 1;
name = 美食;
state = 1;
}
,
{
parentId = 0;
frontName = 住宿;
frontDesc = 住宿;
id = 1073449379929210881;
catClass = 1;
shopCategoryList = (
{
1073449379929210881,
主题酒店,
主题酒店,
1086979050326556673,
2,
,
0,
1,
主题酒店,
1,
}
,
)
;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/7f8d3f37-6de7-4f57-9cb0-0da9a2cc8634.png;
version = 1;
showIndex = 2;
name = 酒店住宿;
state = 1;
}
,
{
parentId = 0;
frontName = 休闲娱乐;
frontDesc = 休闲娱乐;
id = 1073449479862697985;
catClass = 1;
shopCategoryList = (
{
1073449479862697985,
KTV,
KTV,
1078215132553408513,
2,
,
0,
1,
KTV,
1,
}
,
)
;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/e4120430-253d-4d64-83fa-cd5eba22e531.png;
version = 0;
showIndex = 3;
name = 休闲娱乐;
state = 1;
}
,
{
parentId = 0;
frontName = 丽人丽发;
frontDesc = 丽人丽发;
id = 1073449566043062273;
catClass = 1;
shopCategoryList = (
{
1073449566043062273,
美甲美瞳,
美甲美瞳,
1086979452417703938,
2,
,
0,
1,
美甲美瞳,
1,
}
,
)
;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/d9896962-afa0-4014-86cd-e5b39ec71add.png;
version = 1;
showIndex = 4;
name = 丽人丽发;
state = 1;
}
,
{
parentId = 0;
frontName = 按摩足疗;
frontDesc = 按摩足疗;
id = 1073450398151032834;
catClass = 1;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/8c58287a-2abe-43da-bb72-8254941dc877.png;
version = 1;
showIndex = 5;
name = 按摩足疗;
state = 1;
}
,
{
parentId = 0;
frontName = 旅游;
frontDesc = 旅游;
id = 1073451397716590594;
catClass = 1;
shopCategoryList = (
{
1073451397716590594,
旅游,
旅游,
1086980462729068545,
2,
,
0,
1,
旅游,
1,
}
,
)
;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/c8516613-50d6-47f1-add0-3bb9b7a7c6d7.png;
version = 0;
showIndex = 6;
name = 旅游;
state = 1;
}
,
{
parentId = 0;
frontName = 生活服务;
frontDesc = 生活服务;
id = 1073451484584820737;
catClass = 1;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/0b308234-6f32-40e1-9d90-2272151321e8.png;
version = 1;
showIndex = 7;
name = 生活服务;
state = 1;
}
,
{
parentId = 0;
frontName = 百货商城;
frontDesc = 百货商城;
id = 1073451567887892481;
catClass = 1;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/8a4e5e30-83e9-4dce-a3c3-d352114dcdb3.png;
version = 1;
showIndex = 8;
name = 百货商城;
state = 1;
}
,
{
parentId = 0;
frontName = 母婴亲子;
frontDesc = 母婴亲子;
id = 1073451811165913090;
catClass = 1;
wapBannerUrl = http://imagejifenfu.oss-cn-hangzhou.aliyuncs.com/wisdom/20181214/71bfb73a-6ac0-4615-9a23-03687d1d99bd.png;
version = 1;
showIndex = 9;
name = 母婴亲子;
state = 1;
}

)

猜你喜欢

转载自blog.csdn.net/weixin_33971130/article/details/87585556
今日推荐