簡體   English   中英

從字典數組中刪除鍵值的重復項

[英]Removing duplicates of a key value from an array of dictionaries

我正在做一個Facebook API請求,以便從特定的Facebook群組中返回所有相冊的名稱。 我找回了一個包含3個鍵/值的字典數組,其中一個鍵是映射到專輯名稱的鍵'name',以及鍵'id'和'created_time'。

唯一的問題是,由於某些原因,我正在找回專輯的重復“名稱”值...但只有一對。 當我進入Facebook頁面時,無論如何只有該專輯的一個實例,沒有重復。

此外,他們的'id'值是不同的,但它只是重復組中的第一個字典,其具有實際指向有效數據的Facebook ID,其他Facebook id值在您執行Facebook圖形時不會返回任何內容用它們搜索,所以它是我想要的第一個重復項。

如何從我的數組中刪除這些無用的重復詞典,並保持一個有效的Facebook ID? 謝謝! :)

首先,我想說,找到一種從faceBook獲取“干凈”列表的方法可能更有利,而不是事后掩蓋問題。 這可能現在不可能,但至少要找出這種行為的原因或提交錯誤報告。

除此之外,這應該可以解決問題:

-(NSMutableArray *) groupsWithDuplicatesRemoved:(NSArray *)  groups {
    NSMutableArray * groupsFiltered = [[NSMutableArray alloc] init];    //This will be the array of groups you need
    NSMutableArray * groupNamesEncountered = [[NSMutableArray alloc] init]; //This is an array of group names seen so far

    NSString * name;        //Preallocation of group name
    for (NSDictionary * group in groups) {  //Iterate through all groups
        name =[group objectForKey:@"name"]; //Get the group name
        if ([groupNamesEncountered indexOfObject: name]==NSNotFound) {  //Check if this group name hasn't been encountered before
            [groupNamesEncountered addObject:name]; //Now you've encountered it, so add it to the list of encountered names
            [groupsFiltered addObject:group];   //And add the group to the list, as this is the first time it's encountered
        }
    }
    return groupsFiltered;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM