2

How to get startTime and endTime of an Object inside of an Array?

<ion-col *ngFor="let col of timetable" >
        <ion-row  *ngFor="let item in col">
                   {{item.StartTime}}-{{item.EndTime}}<br>{{item.SubjectName}}
       </ion-row>
</ion-col> 

I want to display in the HTML each data in one column and different row for every array of objects. I have done the HTML part, but its not showing me the data and also I have achieved this through TypeScript nested for loop then it and it printed different objects:

for (let temp of temptab) {
     for (let tmp of temp) {
         console.log(JSON.stringify(tmp));
     }
}

Output :

{
"startTime" : 08:30:00,
"endTime" : 08:55:00,
"subjectName" : English
}

My JSON data :

{
  "response": "OK",
  "res": true,
  "timetable": [
    [
      {
        "startTime": "08:30:00",
        "endTime": "08:55:00",
        "subjectName": "English"
      },
      {
        "startTime": "09:00:00",
        "endTime": "09:55:00",
        "subjectName": "English"
      },
      {
        "startTime": "10:00:00",
        "endTime": "10:55:00",
        "subjectName": "Spanish/French"
      },
      {
        "startTime": "11:00:00",
        "endTime": "11:55:00",
        "subjectName": "ICT"
      },
      {
        "startTime": "12:00:00",
        "endTime": "12:45:00",
        "subjectName": "Lunch break"
      }
    ]
]
4
  • what do you mean. I didnt get what you want as result? Commented Feb 14, 2018 at 12:24
  • How to get startTime and Endtime in ionic2? Commented Feb 14, 2018 at 12:29
  • This is an object in an Array in an Array timetable[0][0].startTime, a working example in this fiddle Commented Feb 14, 2018 at 12:46
  • See My edited question Commented Feb 14, 2018 at 13:16

1 Answer 1

1

There's some errors in your code, most of then are syntax errors.

1 - Your second *ngFor is wrong, it's not let item in col it must be let item of col or it'll throw an error saying the property doesn't know *ngForIn because it's an *ngForOf.

2 - The properties name are wrong, in your JSON they start with lowercase letters (startTime) and in your template you're accessing them with uppercase ({{item.StartTime}}), so it can't access since it doesn't exists.

3 - Your grid is wrong, the row must come first and then the col, so it must be:

<ion-row>
  <ion-col></ion-col>
</ion-row>

Also you need to add the column attributes so it can have the desired size, check the grid docs to learn how to do this.

4 - This is not an error since i can't see your full code, so i'm assuming you've passed the timetable of your JSON response to a separated property/variable. But if not you must change your first *ngFor to reflect it so it can be something like *ngFor="let col of myVariable.timetable".

Hope this helps.

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

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.