Home > Software design >  How to remove multiple key:value pair within an array in groovy?
How to remove multiple key:value pair within an array in groovy?

Time:02-12

How to remove/delete key:value pair within a map in groovy?

My code:

data.each { val ->
   SKILLS = val.skills
   HOBBY = val.hobby
   VALUE = val
   NEW_MAP = VALUE - (SKILLS   HOBBY)
}
echo "${VALUE}"
echo "${NEW_MAP}"

output in ${VALUE}, output in ${NEW_MAP} is null

[name: John, skills:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], hobby:[hiking, hockey]]

I also tried this but it wont work:

def x = SKILLS   HOBBY
NEW_MAP = VALUE.minus(x)
NEW_MAP = VALUE.remove(x)

I want to remove the entire skills and hobby. How can I do that?

CodePudding user response:

I would imagine your data is a json, similar to the following structure:

    {
        "data": [
            {
                "skill": [
                    {
                        "singing": "beginner",
                        "dancing": "beginner"
                    },
                    {
                        "java": "competent",
                        "groovy": "beginner"
                    }
                ],
                "hobby": [
                    "hiking",
                    "hockey"
                ]
            },
            {
                "skill": [
                    {
                        "singing": "beginner1",
                        "dancing": "beginner1"
                    },
                    {
                        "java": "competent1",
                        "groovy": "beginner1"
                    }
                ],
                "hobby": [
                    "hiking1",
                    "hockey1"
                ],
                "skill": [
                    {
                        "singing": "beginner2",
                        "dancing": "beginner2"
                    },
                    {
                        "java": "competent2",
                        "groovy": "beginner2"
                    }
                ],
                "hobby": [
                    "hiking2",
                    "hockey2"
                ]
            },
            {
                "skill": [
                    {
                        "singing": "beginner3",
                        "dancing": "beginner3"
                    },
                    {
                        "java": "competent3",
                        "groovy": "beginner3"
                    }
                ],
                "hobby": [
                    "hiking3",
                    "hockey3"
                ],
                "skill": [
                    {
                        "singing": "beginner4",
                        "dancing": "beginner4"
                    },
                    {
                        "java": "competent4",
                        "groovy": "beginner4"
                    }
                ],
                "hobby": [
                    "hiking4",
                    "hockey4"
                ]
            }
        ]
    }
]

when you print the above on the console you have what you have pasted in your question.

[[data:[[skill:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], hobby:[hiking, hockey]], [skill:[[singing:beginner2, dancing:beginner2], [java:competent2, groovy:beginner2]], hobby:[hiking2, hockey2]], [skill:[[singing:beginner4, dancing:beginner4], [java:competent4, groovy:beginner4]], hobby:[hiking4, hockey4]]]]]

If I understand it correctly what you need, you can try something like the below. Where dataJson is the aforementioned json.

JsonSlurper js = new JsonSlurper()
def json = js.parseText(dataJson)
println(json)


def newArr = []
json.data[0].each { val ->
    def new_map = [:]
    new_map.SKILL = val.skill
    new_map.HOBBY = val.hobby
    def value = val

    newArr.add(new_map)
    println("\n"   "This is val: "   value)
    println("This is the map: "   new_map.SKILL   ", "   new_map.HOBBY)

}

println("\n\nThis is the newArr: "   newArr)

Output:

[[data:[[skill:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], hobby:[hiking, hockey]], [skill:[[singing:beginner2, dancing:beginner2], [java:competent2, groovy:beginner2]], hobby:[hiking2, hockey2]], [skill:[[singing:beginner4, dancing:beginner4], [java:competent4, groovy:beginner4]], hobby:[hiking4, hockey4]]]]]

This is val: [skill:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], hobby:[hiking, hockey]]
This is the map: [SKILL:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], HOBBY:[hiking, hockey]]

This is val: [skill:[[singing:beginner2, dancing:beginner2], [java:competent2, groovy:beginner2]], hobby:[hiking2, hockey2]]
This is the map: [SKILL:[[singing:beginner2, dancing:beginner2], [java:competent2, groovy:beginner2]], HOBBY:[hiking2, hockey2]]

This is val: [skill:[[singing:beginner4, dancing:beginner4], [java:competent4, groovy:beginner4]], hobby:[hiking4, hockey4]]
This is the map: [SKILL:[[singing:beginner4, dancing:beginner4], [java:competent4, groovy:beginner4]], HOBBY:[hiking4, hockey4]]


This is the newArr: [[SKILL:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], HOBBY:[hiking, hockey]], [SKILL:[[singing:beginner2, dancing:beginner2], [java:competent2, groovy:beginner2]], HOBBY:[hiking2, hockey2]], [SKILL:[[singing:beginner4, dancing:beginner4], [java:competent4, groovy:beginner4]], HOBBY:[hiking4, hockey4]]]
  • Related