繁体   English   中英

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

[英]Get index of object from array of dictionary with key value

我有一个带字典的数组。

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

像 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" 的对象索引

您可以为此使用indexOf(_:)

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

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

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

您可以在此处查看此操作。

这是使用 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

我首先将 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)
}

其他主流方式来做到这一点

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。

此答案适用于 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM