Мне на выходе дается такой массив (картинка ниже), как обратиться к элементам? Через цикл for не получается...
UPD
export class SocialBlockComponent implements AfterViewInit, OnInit {
friends: FriendBirthdayPreview[] = [];
countBirthdays;
ngOnInit() {
if (this.authenticationService.isLoggedIn()) {
try {
this.loadBirthdays();
this.countNewBirthdays();
} catch (Error) {
console.log(Error.message);
}
}
}
loadBirthdays() {
this.friends = [];
this.friendService.getFriendsBirthdays(this.profileService.getProfileInfo().id).subscribe(
(response) => {
for (let i = 0; i < response.length; i++) {
response[i]['checkedBirthday'] = false;
const friend: FriendBirthdayPreview = new FriendBirthdayPreview;
Object.assign(friend, response[i]);
this.friends.push(friend);
}
}
);
}
countNewBirthdays() {
console.log(this.friends); // Выводит то, что показано в скриншоте
for (let i = 0; i < this.friends.length; i++) {
console.log(this.friends[i]); // Ничего не выводит
}
}
Проблема в этой части кода:
this.loadBirthdays();
this.countNewBirthdays();
Вы вызываете countNewBirthdays, еще до того как пришел ответ от сервера, вы должны переместить countNewBirthdays в subscribe после цикла for:
for (let i = 0; i < response.length; i++) {
response[i]['checkedBirthday'] = false;
const friend: FriendBirthdayPreview = new FriendBirthdayPreview;
Object.assign(friend, response[i]);
this.friends.push(friend);
}
this.countNewBirthdays();
Сборка персонального компьютера от Artline: умный выбор для современных пользователей