angular - 如何在订阅和 ngOnInit 方法中循环遍历数组?
问题描述
我有一个问题,我想遍历 ngOnInit() 方法中的 .subscribe() 方法:
ngOnInit() {
this.service.getEmployees().subscribe(
(listBooks) => {
this.books = listBooks
var events: CalendarEvent[] = [
{
start: new Date(this.books[0].date_from_og), //loop instead of 0
end: new Date(this.books[0].date_to_og),
title: "" + this.books[0].device + "",
color: colors.yellow,
actions: this.actions,
resizable: {
beforeStart: true,
afterEnd: true
},
draggable: true
}];
this.events = events;
},
(err) => console.log(err)
);
}
我想遍历 books[] 数组并推送 events[] 数组中的每个项目,但我不知道如何
解决方案
然后,您可以只迭代 books 数组:
ngOnInit() {
this.service
.getEmployees()
.subscribe(
(listBooks) => {
this.books = listBooks;
this.events = this.books.map((book) => {
return {
start: new Date(book.date_from_og), // use the book (current element in the iteration) directly here
end: new Date(book.date_to_og),
title: "" + book.device + "",
color: colors.yellow,
actions: this.actions,
resizable: {
beforeStart: true,
afterEnd: true
},
draggable: true
};
});
},
(err) => console.log(err)
);
}
推荐阅读
- reactjs - ref object has value for the first time, but ref.current is null
- interface - 在 UML 中用棒棒糖符号表示接口泛化
- vue.js - 将字符串传递给 boostrap/vue 组件的问题
- gradle - 使用 bnd 将 jar 目录包含到 bundle 中
- wagtail - RichText 预览或摘要内容
- sql - 将 Inf、NAN 和 -Inf 插入 sqlite3 数据库的 SQL 命令
- ios - 计划的通知在 iOS Flutter 上不起作用
- java - Midi 设备检索按下的音符
- c# - ASP.NETCORE 在 ConfigureServices 中使用功能标志
- jquery - 仅在按下输入键后刷新jquery自动完成源