从具有键值的字典数组中获取对象的索引

时间:2016-06-02 21:05:22

标签: ios swift

我有一个带字典的数组。

现在我想得到具有特定键值的对象的索引号。

喜欢key =“xyz”和value =“abc”。

我需要在字典中具有上述匹配的对象的索引。

{
        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;
    }

现在我需要具有key =“Title”和value =“result”

的对象索引

5 个答案:

答案 0 :(得分:8)

您可以使用indexOf(_:)

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

在Swift 3.0中,它已重命名为index(Where:)

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

您可以在行动here中看到这一点。

答案 1 :(得分:3)

以下是使用json解析器后应该做的事情

 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 :(得分:0)

我首先将id值映射到数组,然后获取您正在寻找的id的索引。

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

答案 3 :(得分:0)

其他主流方式做到这一点

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)

这将打印1。

答案 4 :(得分:0)

此答案适用于Swift 5.2及更高版本。

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

这是字典格式的数组。如果要查找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)