2

I have an object named: mapped_errors that returns a dynamic object.

sometimes its value is:

{ _id:
   { location: 'body',
     param: '_id',
     value: 123,
     msg: '_id is required and must be a string' } }

and sometimes its value is:

{ _name:
   { location: 'body',
     param: '_name',
     value: 123,
     msg: '_name is required and must be a string' } }

and i want to output the msg value something like this:

let obj_key = Object.keys(mapped_errors);
console.log(mapped_errors.{obj_key}.msg); // i know this is wrong

;-)

5 Answers 5

1

The only error in your code is mapped_errors.{obj_key}.msg. You should be using [] to access the object's property value when you are using a variable to get the key name, as mapped_errors[obj_key].

var mapped_errors = { _id:
   { location: 'body',
     param: '_id',
     value: 123,
     msg: '_id is required and must be a string' } };

let obj_key = Object.keys(mapped_errors);
console.log(mapped_errors[obj_key].msg);

mapped_errors = { _name:
   { location: 'body',
     param: '_name',
     value: 123,
     msg: '_name is required and must be a string' } }
     
obj_key = Object.keys(mapped_errors);
console.log(mapped_errors[obj_key].msg);

Sign up to request clarification or add additional context in comments.

2 Comments

Oh i forgot that way. Haha. Thank you sir.
@noyruto88 glad to help
0

Just loop your object using its keys:

Object.keys(mapped_errors).forEach(key => {
  const value = mapped_errors[key];

  console.log(value.msg);
})

Or if you don't want to loop because there will be just one entry, try with:

const key = Object.keys(mapped_errors)[0];

console.log(mapped_errors[key].msg);

Comments

0

You can check if the property exists mapped_errors.hasOwnProperty('_name')

You coud also access the first key like so: var keys = Object.keys(mapped_errors); console.log(mapped_errors[keys[0]])

Hope this helps

Comments

0

You can use Object.keys with index as per below

mapped_errors = { _id:
   { location: 'body',
     param: '_id',
     value: 123,
     msg: '_id is required and must be a string' } };
     
console.log(mapped_errors[Object.keys(mapped_errors)[0]].msg);     
     
mapped_errors = { _name:
   { location: 'body',
     param: '_name',
     value: 123,
     msg: '_name is required and must be a string' } };

console.log(mapped_errors[Object.keys(mapped_errors)[0]].msg);

Object.keys returns the array of keys [0] will take first key and mapped_errors[Object.keys(mapped_errors)[0]].msg will return msg content.

Comments

0

let obj = { _id:
   { location: 'body',
     param: '_id',
     value: 123,
     msg: '_id is required and must be a string' } }

console.log(obj[Object.keys(obj)[0]].msg)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.