我知道如何分别完成这两件事,但我相信一定有一种方法可以将它们结合起来。
我有一个类别数组,我从一个对象数组中提取它们:
this.videoCategories = this.videos.map(v => v.category);
当然,这个数组中有重复的元素。因此,现在我执行以下操作:
this.uniqueVideoCategories = this.videoCategories.filter((item, index) => {
return this.videoCategories.indexOf(item) === index;
});
这个方法可以正常工作,我得到一个没有重复项的分类数组。但我试图学习并使代码更简洁一些,将它们串联起来,但这不起作用-返回空数组。
constructor(private videoService: VideoService) {
this.videos = videoService.getVideos();
this.videoCategories = this.videos
.map(v => v.category)
.filter((item, index) => {
return this.videoCategories.indexOf(item) === index;
});
console.log(this.videoCategories);
}