0

I have the following object...

var object = doc.$set;
console.log(object);

The console.log above outputs the following...

{
  createdBy: 'o5Wye6LLMGNXLn7HY',
  createdAt: Mon Apr 11 2016 15:25:35 GMT+1000 (AEST), 
  'contactDetails.orderedBy': 'MvCun8p6vxndj3cr8',
  updatedAt: Mon Apr 11 2016 18:04:14 GMT+1000 (AEST)
}

How can I get the value from 'contactDetails.orderedBy'?

Where something like this works no problem...

var createdBy = doc.$set.createdBy;

But this doesn't...

var orderedBy = doc.$set.contactDetails.orderedBy;

Is there a way that I can use javascript to extract the value instead? Like maybe convert it to a string and then split it or something along those lines? Is this possible?

Thanks

Serks
  • 333
  • 2
  • 21

2 Answers2

3

Try var orderedBy = doc.$set['contactDetails.orderedBy'];.

Since an object key is a numeric literal or a valid identifier name, it is necessary to quote the key contactDetails.orderedBy to avoid a syntax error.

Quotes can only be omitted if the property name is a numeric literal or a valid identifier name.

In the case of createdBy key, you could retrieve the value like object.createdBy but in the case of contactDetails.orderedBy you need to quote, so access it via object[contactDetails.orderedBy].

Serks
  • 333
  • 2
  • 21
Lionel T
  • 1,559
  • 1
  • 13
  • 30
0

If you want to manipulate object than you need understand the concept of key and value

see this examples

var object={
 "iAmFirstKey":'I am value',
 "i.am.second.key":'i am value'
} 

so In your case your contacDetails.orderedBy is not Object inside Object it is an key ("string")

so use like this doc.$set['contacDetails.orderedBy']; you will get your value

Keval Bhatt
  • 6,224
  • 2
  • 24
  • 40