简体   繁体   中英

compare two arrays with not all similar entries and store similar components in third array ios

I have an array arrAll which has a dictionary at each index. So, arrAll is of the form

arrAll = ({"id"=1,"type"="first",},{"id"=2,"type"="second"},{"id"=3,"type"="third"},{"id"=4,"type"="fourth"});

I have another array named arrSelected.

arrSelected = (second,fourth);

Now, what i have to do is pick the dictionaries containing 'second' & 'fourth' as their type and store them in my third array, say, arrFiltered. So ultimately arrFiltered should contain

arrFiltered = ({"id"=2,"type"="second"},{"id"=4,"type"="fourth"});

It could be easily done using conventional method of looping. I want to ask if there is any other way out to get it done using PREDICATE or something like INTERSECTSET?

For Swift you are looking for something like:

let arrAll: [[String: Any]] = [
        ["id": 1, "type": "first"],
        ["id": 2, "type": "second"],
        ["id": 3, "type": "third"],
        ["id": 4, "type": "fourth"]
    ]

let arrSelected = ["second", "fourth"]

let arrFiltered = arrAll.filter() {
    guard let type = $0["type"] as? String else { return false }
    return arrSelected.contains(type)
}

print(arrFiltered) // [["id": 2, "type": "second"], ["id": 4, "type": "fourth"]]

Alternative Swift version emanating from arrSelected rather than arrAll . Pre-requisite that the value for key "type" is unique in each dictionary in the array (otherwise: just collects first such dictionary):

let arrAll : [[String:Any]] = [
    ["id": 1, "type": "first"],
    ["id": 2, "type": "second"],
    ["id": 3, "type": "third"],
    ["id": 4, "type": "fourth"]]

let arrSelected = ["second","fourth"]

/* Alternative Swift method: from arrSelected -> filtered */
let arrFiltered = arrSelected.flatMap {
    v in arrAll.flatMap { ($0["type"] as? String) == v ? $0 : nil }.first
}

print(arrFiltered)
    /* [["id": 2, "type": "second"], ["id": 4, "type": "fourth"]] */

Try out this code for objective-C by using conventional looping method

    NSMutableArray * arrFiltered =[[NSMutableArray alloc]init];
    for (int i=0; i<arrAll.count; i++)
    {
        NSDictionary *dictFirst=[arrAll objectAtIndex:i];
        for (int j=0; j< arrSelected.count; j++)
        {
            NSString *strSecond=[arrSelected objectAtIndex:j];
            if ([[dictFirst valueForKey:@"type"]isEqualToString:strSecond])
            {
                [arrFiltered addObject:[arrAll objectAtIndex:i]];
            }
        }
    }
    NSLog(@"Flitered Array= %@",arrFiltered);

you can also you predicate to get filtered array by using below code

 NSMutableArray * arrFiltered =[[NSMutableArray alloc]init];
 for (int j=0; j< arrSelected.count; j++)
 {
        NSString *strSecond=[arrSelected objectAtIndex:j];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"type =%@",strSecond];
        NSArray *arrTemp=[arrAll filteredArrayUsingPredicate:predicate];
        [arrFiltered addObjectsFromArray:arrTemp];
  }
  NSLog(@"Flitered Array= %@",arrFiltered);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM