8

Hello guys i have an array of dictionary, can you guys tell me how can i filter this data based on dictionary keys.

(
    {
    "mall_id" = M0550;
    "mall_name" = "Amrita Shopping Complex";
},
    {
    "mall_id" = M0509;
    "mall_name" = "Ashoka Market";
},
    {
    "mall_id" = M0943;
    "mall_name" = "Biju Pattnaik Commercial Complex";
},
    {
    "mall_id" = M0457;
    "mall_name" = "BMC Bhawani Mall";
},
    {
    "mall_id" = M0460;
    "mall_name" = "BMC Keshari Mall";
},
    {
    "mall_id" = M0571;
    "mall_name" = "BMC Market Complex";
},
    {
    "mall_id" = M0453;
    "mall_name" = "Forum Mart";
},
    {
    "mall_id" = M0609;
    "mall_name" = "Indradhanu Market";
},
    {
    "mall_id" = M0558;
    "mall_name" = "Kalyani Plaza Market Complex";
},
    {
    "mall_id" = M0463;
    "mall_name" = "Maa Barabhuja Mall";
},
    {
    "mall_id" = M0553;
    "mall_name" = "Mahaveer Complex";
},
    {
    "mall_id" = M0570;
    "mall_name" = "Market Building";
},
    {
    "mall_id" = M0452;
    "mall_name" = "Pal Heights Mall";
},
    {
    "mall_id" = M0466;
    "mall_name" = "Priyadarshini Market Complex";
},
    {
    "mall_id" = M0677;
    "mall_name" = "Ruchika Market";
},
    {
    "mall_id" = M0504;
    "mall_name" = "Shubham Market Complex";
},
    {
    "mall_id" = M0564;
    "mall_name" = "Subhadra Complex";
},
    {
    "mall_id" = M0559;
    "mall_name" = "Sultania Shopping Complex";
},
    {
    "mall_id" = M0552;
    "mall_name" = "Tathastu Complex";
},
    {
    "mall_id" = M0568;
    "mall_name" = "Western Tower Market Building";
}
)

what i want to achieve, whenever i type anything in search bar it will check mall_name key and return matching values in array.

Thanks and Regards

sanjeet
  • 1,539
  • 15
  • 37

9 Answers9

20

This will give you your desired output

Objective - C

NSArray *filteredData = [yourArrayContainingDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];

Swift

let filteredData = yourArrayContainingDictionary.filter{
    let string = $0["mall_name"] as! String

    return string.hasPrefix("searchText")
}

Hope this helps you :)

iOS_MIB
  • 1,885
  • 13
  • 24
11

Try this one. (Predicate works like SQL queries)

Obj C

 NSArray *filterArray = [sourceArray filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]];

Swift

var filterArray: [Any] = sourceArray.filter { NSPredicate(format: "(mall_name contains[c] %@)", searchText).evaluate(with: $0) }

It will return entries whose name contain the search string.

Tejas Ardeshna
  • 4,343
  • 2
  • 20
  • 39
4

Use this for you solution

NSArray *filtered = [yourArrayOfDictionary filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", <your search text from search bar>]];
PSS
  • 316
  • 1
  • 9
2

Here are my 2 versions using predicate and the classical one:

let dict = [
    [
        "mall_id": 1,
        "mall_name": "Amrita Shopping Complex"
    ],
    [
        "mall_id": 2,
        "mall_name": "Ashoka Market"
    ] ]

// Example using Predicate

let mallNamePredicate = NSPredicate(format: "mall_name contains %@", "Ashoka")
let filteredWithPredicate = (dict as NSArray).filtered(using: mallNamePredicate)

// Classical filter example 
let filtered = dict.filter { pair in
    guard let mallName = pair["mall_name"] as? String else { return false }
    return mallName.hasPrefix("Ashoka")
}
Oleg Danu
  • 4,149
  • 4
  • 29
  • 47
1

this gives you an array of dictionaries with requiredMallID only

 NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", requiredMallID]];

For example,

NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", "M0550"]];

gives you

    (
        {
        "mall_id" = M0550;
        "mall_name" = "Amrita Shopping Complex";
    } 
)
Amal T S
  • 3,327
  • 2
  • 24
  • 57
1
NSArray *filtered = [arrName filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_name == %@)", requiredMailName]];
Miti
  • 119
  • 1
  • 7
1
 NSArray *filtered = [array_with_dict filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(mall_id == %@)", wantedMallID]];
Vignesh Davins
  • 285
  • 1
  • 13
1

More swifty way of doing this would be something like

let mallArray  = [
    [
        "mall_id": "M0550",
        "mall_name": "Amrita Shopping Complex"
    ],
    [
        "mall_id": "M0509",
        "mall_name": "Ashoka Market"
    ]
]

func isMatching(_ searchText: String) -> [[String: Any]] {
    let filteredArray = mallArray.filter {
        return $0["mall_name"]!.contains(searchText)
    }
    return filteredArray
}

let malls = isMatching("Ashoka")
print(malls)
Suhit Patil
  • 11,748
  • 3
  • 50
  • 60
0

He wants to search on partial matches on mall_name, not mall_id, continually updating as users type into the search bar. So correct solutions are those with this:

[NSPredicate predicateWithFormat:@"(mall_name contains[c] %@)", searchText]

... not 'mall_id', not @"(xxx == %@)", and the 'contains[c]' gives him case-insensitive matches.

SteveCaine
  • 512
  • 4
  • 13