S
S
slip312017-10-22 11:58:18
JavaScript
slip31, 2017-10-22 11:58:18

How to count the number of objects in an array?

Good afternoon. How to calculate the number? I need to count in an object the quiznumber of objects included in an arraytextCategory

ngOnInit() {
      this.quizService.getData().subscribe((data: Response) => this.quiz=data.json());     
      this.countPages = this.quiz.textCategory.length; // не работает

{ "categoryID": 1,
   "nameCategory": "Первая категория",
   "textCategory":
           [{"textId":1,
           "textContent":"<h3>Первый</h3>"},
           {"textId":2,
           "textContent":"<h3>Второй</h3>"}] 
 }

So gives out
RROR TypeError: Cannot read property 'textCategory' of undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Interreto, 2017-10-22
@slip31

Judging by the text of the error, the problem is not in the calculations, but in the fact that quizService is asynchronous, and there is no object yet
this.countPages = this.quiz.textCategory.length;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question