Get index of object from array of dictionary with

2020-07-27 03:28发布

问题:

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"

回答1:

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


回答2:

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.



回答3:

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)
}


回答4:

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.



标签: ios swift