如何确定数组是否包含具有等于给定值的属性的对象

How to determine if array contains an object with an attribute that equals a given value

我有一个这样的数组,它将来自 API 的响应。

[
    {
        "id": "F1eOrsr3g7gad6",
        "created_at": 1591951315,
        "url": "https://example.com",
        "secret": "1234",
        "secret_exists": true
    },
    {
        "id": "FintcvTBaYwPz2",
        "created_at": 1591953315,
        "url": "http://example.com",
        "secret": "34532",
        "secret_exists": true
    }
]

如何检查此数组是否具有值“F1eOrsr3g7gad6”?

我曾尝试编写一个函数,如果该值存在,它将 return 索引值。

func isExists(key string, value string, data []map[string]interface{}) (result int) {
    result = -1
    for i, search := range data {
        if search[key] == value {
            result = i
            break
        }
    }
    return result
}
var value string = "FintcvTBaYwPz2"
var key string = "id"
result := isExists(key, value, data) // here data will be the array which I want to pass.
fmt.Println("Result: ", result)

您已经完成了所有艰苦的工作,只需将函数的签名更改为 return a bool 而不是索引,并且 return truefalse:

func isExists(key string, value string, data []map[string]interface{}) (exists bool) {

    for _, search := range data {
        if search[key] == value {
            return true
        }
    }
    return false
}

https://play.golang.org/p/lJGwa6pZaX5