R
R
reCAPTCHA2022-03-11 17:45:05
JSON
reCAPTCHA, 2022-03-11 17:45:05

Adding a value to a variable with the same name as the key?

There is a json record with keys the same as variables.
How to add to a variable a number that is equal to the value of a key with the same name as the variable?

{ 
  "x": 3
  "y": 2
  "z": 1
} 

let x = 0
let y = 0
let z = 0


At the moment, I have implemented this with the usual switch design, but I'm sure that there is a better and shorter option:

for (var key in json) {
  switch (key) {
    case "x":
      x += json[key]
      break;
//etc
  }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question