而不是使用
forEach
,使用数组
.map
功能,所以:
this.currentProcessDefinition.day1dates = this.currentProcessDefinition.day1dates.map(dat => new Date(dat))
或者,如果您想在您的服务中这样做,您可以这样做:
import { map } from 'rxjs/operators'
getItems() {
let url = this.apiUrl + this.entityName + '/';
return this.http.get(url).pipe(
// Map through each item in res, and format the object
map((res) => res.map(item => this.formatDates(item)))
)
}
formatDates(results) {
// Map through each date and replace with Date objects
results.day1dates = results.day1dates.map(dat => new Date(dat));
return results;
}
在这段代码中,我们通过它自己的管道
.地图
函数,以转换结果。在组件中,您可以像往常一样订阅。