ananya
ananya

Reputation: 1041

How to get data from [object object] in Angular 6

In console i am getting data as [object Object]. console.log('data is: '+ (data));

data is:  [object Object]

When i am doing json stringify, console.log('data is: '+ JSON.stringify(data)); I am getting data in this format:

{
"employee":
       { "name":"John", 
         "age":30, 
         "city":"New York" 
      }
}

I have 3 variable name , age and city.I want to get those values from this data in ts file.

name: any;
age: any;
city: any;
constructor(private dialogRef: MatDialogRef<EmployeedetailsComponent>,
@Inject(MAT_DIALOG_DATA) data, private dialog: MatDialog){
   this.name = data.name;
}

this.name = data.name; this is not working. How to get these values.Can anyone please help me with this.

Upvotes: 0

Views: 6251

Answers (2)

Shay Levinzon
Shay Levinzon

Reputation: 76

It should be data.employee.name

Upvotes: 2

Hien Nguyen
Hien Nguyen

Reputation: 18975

You need change to this.name = data.employee.name;

 constructor(private dialogRef: MatDialogRef<EmployeedetailsComponent>,
@Inject(MAT_DIALOG_DATA) data, private dialog: MatDialog){
   this.name = data.employee.name;
}

Upvotes: 1

Related Questions