组件 1
this.service.myArray['artists'] = data;
组件 2
@Input
组件二通过一个属性接收每个艺术家 ID 。然后我执行查询以获取所有专辑。每个专辑文档都有一个“artist_id”属性。
@Input() artist_id: string;
----
ngOnInit(): void {
this.albums$ = this.service
.getAlbums$(var1, var2, etc)
.pipe(
filter((data) => !!data),
take(1),
map((data) => {
// logic here?
return data;
})
);
}
如何循环遍历返回的专辑并myArray
根据匹配将它们注入到数组中artist_id
?例如,任何返回的专辑具有artist_id
等于artists
数组中项目的文档 ID (myArray[artists]) 的任何返回的专辑都被推到正确的位置:
myArray:
artists:
- 0 {
title: 'title', 'id': '1234',
albums:
- 0 { 'album 1', artist_id: '1234' }
- 1 { 'album 2', artist_id: '1234' }
},
- 1 {
title: 'title 2', 'id': '4321',
albums:
- 0 { 'album 3', artist_id: '4321' }
- 1 { 'album 4', artist_id: '4321' }
},
更新
我有它的工作。不完全确定它是否优雅。
ngOnInit(): void {
this.albums $ = service
.getAlbums$(properties)
.pipe(
filter((data) => !!data),
take(1),
tap((data) => {
this.updateArray(data);
return data;
})
);
}
updateArray(data) {
data.forEach((album) => {
this.service.myArray["artists"].find((artist) => {
if (artist.id === item.artist_id) {
this.albums.push(album);
artist["albums"] = this.albums;
}
});
});
}