如何删除 groovy 数组中的多个 key:value 对?
How to remove multiple key:value pair within an array in groovy?
如何 remove/delete key:value 在 groovy 中的地图中配对?
我的代码:
data.each { val ->
SKILLS = val.skills
HOBBY = val.hobby
VALUE = val
NEW_MAP = VALUE - (SKILLS + HOBBY)
}
echo "${VALUE}"
echo "${NEW_MAP}"
${VALUE}中的输出,${NEW_MAP}中的输出为空
[name: John, skills:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], hobby:[hiking, hockey]]
我也试过这个但是不行:
def x = SKILLS + HOBBY
NEW_MAP = VALUE.minus(x)
NEW_MAP = VALUE.remove(x)
我想去掉所有的技能和爱好。我该怎么做?
我会想象你的数据是一个json,类似于下面的结构:
{
"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"
]
}
]
}
]
当你在控制台上打印上面的内容时,你就得到了你在问题中粘贴的内容。
[[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]]]]]
如果我理解正确你需要什么,你可以试试下面的方法。其中dataJson
就是前面提到的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)
输出:
[[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]]]
如何 remove/delete key:value 在 groovy 中的地图中配对?
我的代码:
data.each { val ->
SKILLS = val.skills
HOBBY = val.hobby
VALUE = val
NEW_MAP = VALUE - (SKILLS + HOBBY)
}
echo "${VALUE}"
echo "${NEW_MAP}"
${VALUE}中的输出,${NEW_MAP}中的输出为空
[name: John, skills:[[singing:beginner, dancing:beginner], [java:competent, groovy:beginner]], hobby:[hiking, hockey]]
我也试过这个但是不行:
def x = SKILLS + HOBBY
NEW_MAP = VALUE.minus(x)
NEW_MAP = VALUE.remove(x)
我想去掉所有的技能和爱好。我该怎么做?
我会想象你的数据是一个json,类似于下面的结构:
{
"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"
]
}
]
}
]
当你在控制台上打印上面的内容时,你就得到了你在问题中粘贴的内容。
[[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]]]]]
如果我理解正确你需要什么,你可以试试下面的方法。其中dataJson
就是前面提到的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)
输出:
[[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]]]