简体   繁体   中英

How to refer the field from this Javascript object?

I have a object with printout from a console.log() command:

console.log("event in validation ", this.event_info);

Here is the printout:

event in validation  {"start_datetime":"Sun Jul 14 2019 18:20:00 GMT-0700 (PDT)","finish_datetime":"Mon Jul 15 2019 18:20:00 GMT-0700 (PDT)"}

There are 2 fields in this.event_info : start_datetime and finish_datetime . I need to refer the value of finish_datetime and did the following:

this.event_info["finish_datetime"];

and

let sd = "finish_datetime";
this.event_info[sd];

But there are nothing retrieved (undefined or nothing). Then I tried to print out the keys of the object:

 Object.keys(this.event_info);

And printout is:

 [ '0',
  '1',
  '2',
  '3',
  .
  .
  .
  99

Then I tried these:

 this.event_info["1"];

and:

this.event_info[1];

It is nothing printed out again. What is the right way here to refer the value of finish_datetime ?

Long Answer

console.log() is a terrible debugging tool. It does not show data types and what it shows is not always a correct representation of the state of your application at the time you call it .

You should instead use your browser's actual debugger or one connected via your editor / IDE.

Short Answer

Your variable is a JSON string, not an object. You want

JSON.parse(this.event_info).finish_datetime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM