3

Need to display only the Column Name's containing in each row of an array

Eg: From the console

this.GobalTableDataSets
(3) [{…}, {…}, {…}]
0: {centerName: "--", batchName: "MentorSchedule_5D5C", scheduleStartDate: "20-Nov-2019 14:38:17", scheduleEndDate: "20-Dec-2019 14:38:17", …}
1: {centerName: "--", batchName: "MentorSchedule_2657", scheduleStartDate: "20-Nov-2019 10:29:46", scheduleEndDate: "20-Dec-2019 10:29:46", …}
2: {centerName: "--", batchName: "Enroll_cf845bd8-43", scheduleStartDate: "18-Jul-2019 18:32:00", scheduleEndDate: "31-Jul-2019 21:28:00", …}
length: 3

component.ts

this._liveDashboardService.getBatchDetails(orgReqDetailsEx ).subscribe(response => {
   if (response !== null && response.length > 0 && response !== null) {
          this.batchDetails = response;
   }
});

Expected Result:

this.GobalTableDataSets[0].<someFunction>
"centerName"
"batchName"
"scheduleStartDate"
"scheduleEndDate"

Here the requirement is to console only the Column Name's (all or particular name) containing from an above array of each row, but not necessarily displaying/fetching Column Values containing those respective Column Name's from an array

Are there any built-in functions from Angular to accomplish the above scenario?

2 Answers 2

2

Just use Object.keys():

let keys = Object.keys(this.batchDetails[0]);
console.log(keys);

Working example:

var list = [
{centerName: "--", batchName: "MentorSchedule_5D5C", scheduleStartDate: "20-Nov-2019 14:38:17", scheduleEndDate: "20-Dec-2019 14:38:17"},{centerName: "--", batchName: "MentorSchedule_2657", scheduleStartDate: "20-Nov-2019 10:29:46", scheduleEndDate: "20-Dec-2019 10:29:46"}
,{centerName: "--", batchName: "Enroll_cf845bd8-43", scheduleStartDate: "18-Jul-2019 18:32:00", scheduleEndDate: "31-Jul-2019 21:28:00"}
];

console.log(Object.keys(list[0]));

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

1 Comment

It worked same as the old school vanilla JS. Is it possible to achieve the same via Angular?
0

use below code:

component.ts

var keys;
this._liveDashboardService.getBatchDetails(orgReqDetailsEx ).subscribe(response => {
   if (response !== null && response.length > 0 && response !== null) {
          this.batchDetails = response;
          keys = Object.keys(this.batchDetails[0])
          console.log(keys);
   }
});

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.