How to delete duplicate interface data in {}interface{}

PHPz
Release: 2024-02-09 23:12:09
forward
975 people have browsed it

How to delete duplicate interface data in {}interface{}

php editor Youzi will introduce to you how to delete duplicate interface data{} in the {}interface. During the development process, we often encounter situations where we need to delete duplicate data. Duplicate data not only affects system performance, but also causes trouble to users. In order to solve this problem, we can use some techniques and methods to detect and delete duplicate interface data, thereby improving system efficiency and user experience. Next, we’ll give you the details on how to achieve this.

Question content

How to remove duplicate interface{} entries from []interface{} data.

"data": [
        {
            "signstatus": true,
            "username": "yash90"
        },
        {
            "signstatus": false,
            "username": "dhananjay"
        },
        {
            "signstatus": true,
            "username": "yash90"
        }
    ],
Copy after login

Currently, I have the above data and I want to remove duplicate entries for the same username. So what kind of logic do I need in the function below.

func removeduplicateentries(listofusernamesandsignstatus []interface{}) []interface{} {
    updatedlistofusernamesandsignstatus := make([]interface{}, 0)
    for _, value := range listofusernamesandsignstatus {
        if value != nil {
        updatedlistofusernamesandsignstatus = append(updatedlistofusernamesandsignstatus, value)
        }
    }
    return updatedlistofusernamesandsignstatus
}
Copy after login

Any idea how to do this?

I expect the result should be as follows:

"data": [
        {
            "signstatus": true,
            "username": "Yash90"
        },
        {
            "signstatus": false,
            "username": "Dhananjay"
        },
    ],
Copy after login

Workaround

First of all, I think using []interface{} as input is a bad idea in this specific case. The correct input should be []*entry with

type entry struct {
    username   string `json:"username"`
    signstatus bool   `json:"signstatus"`
}
Copy after login

Or at least map[string]interface{}.

But if []interface{} is a must, then here is 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
}
Copy after login

The above is the detailed content of How to delete duplicate interface data in {}interface{}. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!