Kush Thakkar
Kush Thakkar

Reputation: 59

Get index of object from array of dictionary with key value

I have one array with dictionary.

Now i want to get index number of object with particular key value.

Like key = "xyz" and value = "abc".

I need index of object having above matching in dictionary.

{
        Id = 20085;
        IsNew = 1;
        Title = Circular;
    },
        {
        Id = 20088;
        IsNew = 0;
        Title = Query;
    },
        {
        Id = 20099;
        IsNew = 1;
        Title = Blog;
    },
        {
        Id = 20104;
        IsNew = 1;
        Title = News;
    },
        {
        Id = 20172;
        IsNew = 1;
        Title = AssignTask;
    },
        {
        Id = 20183;
        IsNew = 1;
        Title = Gallery;
    },
        {
        Id = 20204;
        IsNew = 1;
        Title = Poll;
    },
        {
        Id = 20093;
        IsNew = 1;
        Title = Assignment;
    },
        {
        Id = 20209;
        IsNew = 1;
        Title = Activity;
    },
        {
        Id = 20130;
        IsNew = 1;
        Title = Behaviour;
    },
        {
        Id = 20180;
        IsNew = 1;
        Title = Result;
    }

now i need index of object with having key = "Title" and value = "result"

Upvotes: 2

Views: 13322

Answers (5)

Shrikant Phadke
Shrikant Phadke

Reputation: 418

This answer is for the Swift 5.2 and above versions.

 arr.append(["Title":titleName,"ID":1,"Result":true])
 arr.append(["Title":titleName,"ID":2,"Result":true])
 arr.append(["Title":titleName,"ID":3,"Result":true])
 arr.append(["Title":titleName,"ID":4,"Result":true])

This is an array of dictionary format. And if you want to find an index of an object where ID == 3

func findIndex(Id : Int ) -> Int? {
    
    guard let index = arr.firstIndex(where: {
        if let dic = $0 as? Dictionary<String,AnyObject> {
            if let value = dic["ID"]  as? Int, value == Id {
                
                return true
            }
            
        }
        return  false
    }) else { return nil }
    return index
}

let myIndex = findIndex(3)
self.arr.insert( ["Title":titleName,"ID":10,"Result":true]) at: myIndex)

Upvotes: 0

SUMIT NIHALANI
SUMIT NIHALANI

Reputation: 417

other mainstream way to do this

func getIndex(of key: String, for value: String, in dictionary : [[String: Any]]) -> Int{
    var count = 0
    for dictElement in dictionary{
        if dictElement.keys.contains(key) && dictElement[key] as! String == value{
            return count
        }
        else{
            count = count + 1
        }
    }
    return -1

}

var sampleDict : [[String:Any]] = [
    [
        "Id" : 20130,
        "IsNew" : 1,
        "Title" : "Behaviour"
    ],
    [
        "Id" : 20130,
        "IsNew" : 1,
        "Title" : "Result"
        ],
]

let index = getIndex(of: "Title", for: "Result", in: sampleDict)

print(index)

This will print 1.

Upvotes: 0

James Wolfe
James Wolfe

Reputation: 350

I'd start with mapping the id values to an array, then getting the index of the id you're looking for.

func getIndexOf(itemID: Int) -> Int {

    //Map out all the ids from the array of dictionaries into an array of Integers
    let keysArray = dictionaries.map { (dictionary) -> Int in
        return dictionary.value(forKey: "Id")
    }

    //Find the index of the id you passed to the functions
    return keysArray.index(of: itemID)
}

Upvotes: 0

Alexander
Alexander

Reputation: 63272

You can use indexOf(_:) for this:

let index = array.indexOf{ $0["key"] == value }

In Swift 3.0, it's been renamed to index(Where:):

let index = array.index{ $0["key"] == value }

You can see this in action here.

Upvotes: 8

Cjay
Cjay

Reputation: 1093

Here is what you should be doing after using a json parser

 let array:NSArray = [
[
    "Id": 20130,
    "IsNew": 1,
    "Title":"Behaviour"
],
[
    "Id": 20180,
    "IsNew": 1,
    "Title":"Result"
]]


let k = array as Array
let index = k.indexOf {
    if let dic = $0 as? Dictionary<String,AnyObject> {
        if let value = dic["Title"]  as? String
            where value == "Result"{
         return true
        }
    }
    return false
}

print(index) // index

Upvotes: 3

Related Questions