0

I want to define an object value as a new variable. How can I achieve this? Here's an example:

var msg = '{
  "fname": {
    "req": "first name required"
  },
  "email": {
    "req": "email is required",
    "validate": "provide valid email"
  }
}';
var obj = JSON.parse(msg);

console.log(msg.fname.req) // logs "first name required", that's fine

This is what I tried so far:

 new_fname = 'fname'
 console.log(msg.new_fname.req) // this throws an error ("req is not defined")

How can I declare the new_fname value as a new variable?

hawc
  • 328
  • 2
  • 14
Jayesh Vekariya
  • 163
  • 1
  • 1
  • 6

0 Answers0