I'm primarily a JavaScript developer, but am currently working on some Groovy code and haven't been able to figure out how to do something that's super-simple in JavaScript.
The JavaScript equivalent of what I'm trying to do follows.
I'm specifically trying to figure out the Java (or Groovy) equivalent of creating an object in JS (map in Java) out of just the existing variable names, e.g. {a, b, c}
shorthand in the code snippet below. Any guidance will be much appreciated!
javaScriptExample()
function javaScriptExample () {
// the variables already exist in the program that I'm working in
const a = 'a'
const b = 'bee'
const c = 'see'
// ?? Here's where I'm stuck. ??
// I simply want to be able to arbitrarily pass variable keys and values
// as a map to another function, _using just the variable keys_,
// e.g. the equivalent of JavaScript's `{a, b, c}` in the next call
doOtherStuffWithVariables({a, b, c})
}
function doOtherStuffWithVariables (obj) {
for (const key in obj) {
console.log(`variable ${key} has a value of "${obj[key]}" and string length of ${obj[key].length}`)
}
}
question from:https://stackoverflow.com/questions/65906680/create-a-map-from-existing-variables-e-g-a-java-equivalent-to-javascripts-v