英文:
How to remove duplicate {}interface data from []interface{}
问题
如何从 []interface{} 数据中删除 interface{} 的重复条目。
"data": [
{
"signstatus": true,
"username": "Yash90"
},
{
"signstatus": false,
"username": "Dhananjay"
},
{
"signstatus": true,
"username": "Yash90"
}
],
目前,我有上述数据,并且我想删除相同用户名的重复条目。所以在下面的函数中,我需要什么样的逻辑。
func removeDuplicateEntries(listOfUserNamesAndSignStatus []interface{}) []interface{} {
updatedListOfUserNamesAndSignStatus := make([]interface{}, 0)
for _, value := range listOfUserNamesAndSignStatus {
if value != nil {
updatedListOfUserNamesAndSignStatus = append(updatedListOfUserNamesAndSignStatus, value)
}
}
return updatedListOfUserNamesAndSignStatus
}
有什么想法如何做到这一点?
我期望的结果应该如下所示:
"data": [
{
"signstatus": true,
"username": "Yash90"
},
{
"signstatus": false,
"username": "Dhananjay"
},
],
英文:
How to remove duplicate entries of interface{} from []interface{} data.
"data": [
{
"signstatus": true,
"username": "Yash90"
},
{
"signstatus": false,
"username": "Dhananjay"
},
{
"signstatus": true,
"username": "Yash90"
}
],
Currently, I have the above data and I want to remove duplicate entries of the same username. So what kind logic I need in below function.
func removeDuplicateEntries(listOfUserNamesAndSignStatus []interface{}) []interface{} {
updatedListOfUserNamesAndSignStatus := make([]interface{}, 0)
for _, value := range listOfUserNamesAndSignStatus {
if value != nil {
updatedListOfUserNamesAndSignStatus = append(updatedListOfUserNamesAndSignStatus, value)
}
}
return updatedListOfUserNamesAndSignStatus
}
Any idea How to do that?
I expect the result should be as follows:
"data": [
{
"signstatus": true,
"username": "Yash90"
},
{
"signstatus": false,
"username": "Dhananjay"
},
],
答案1
得分: 1
首先,我认为在这种特定情况下使用[]interface{}
作为输入是一个不好的主意。一个合适的输入应该是[]*Entry
,其中Entry
的定义如下:
type Entry struct {
Username string `json:"username"`
Signstatus bool `json:"signstatus"`
}
或者至少是map[string]interface{}
。
但是如果[]interface{}
是必需的,那么下面是解决方案。
func removeDuplicateEntries(entries []interface{}) []interface{} {
uniqueEntries := make([]interface{}, 0)
usernameMap := make(map[string]bool)
for _, entry := range entries {
entryCast, ok := entry.(map[string]interface{})
if !ok {
continue
}
username := entryCast["username"].(string)
if usernameMap[username] {
continue
}
usernameMap[username] = true
uniqueEntries = append(uniqueEntries, entry)
}
return uniqueEntries
}
英文:
First of all, I think using []interface{}
as input in this specific case is a bad idea. A proper input should be []*Entry
with
type Entry struct {
Username string `json:"username"`
Signstatus bool `json:"signstatus"`
}
or at least map[string]interface{}
.
But if []interface{}
is a must then here's the solution.
func removeDuplicateEntries(entries []interface{}) []interface{} {
uniqueEntries := make([]interface{}, 0)
usernameMap := make(map[string]bool)
for _, entry := range entries {
entryCast, ok := entry.(map[string]interface{})
if !ok {
continue
}
username := entryCast["username"].(string)
if usernameMap[username] {
continue
}
usernameMap[username] = true
uniqueEntries = append(uniqueEntries, entry)
}
return uniqueEntries
}
答案2
得分: 0
你可以通过使用Equal
接口来比较struct
类型的变量,而不是interface{}
类型的变量。我发现没有办法比较两个interface{}
类型的变量。
type Userinfo struct {
Signstatus bool `json:"signstatus"`
Username string `json:"username"`
}
func (user *Userinfo) Equal(target *Userinfo) bool {
return user.Signstatus == target.Signstatus && user.Username == target.Username
}
以上是你提供的代码的翻译。
英文:
you can compare via Equal interface if you use struct instead of {}interface, i found there is no way to compare two viraibles of {}interface type
type Userinfo struct {
Signstatus bool `json:"signstatus "`
Username string `json:"username"`
}
func (user *Userinfo) Equal(target *Userinfo) bool {
return user.Signstatus == target.Signstatus && user.Username == target.Username
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论