【发布时间】:2021-10-04 11:06:02
【问题描述】:
组件 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;
})
);
}
如何循环通过我返回的专辑并根据匹配的artist_id 将它们注入myArray 数组?例如,任何返回的专辑,其 artist_id 等于 artists 数组 (myArray[artists]) 中项目的文档 ID,都会被推送到正确的位置:
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;
}
});
});
}
【问题讨论】:
-
thread 是否回答了您的问题?
-
你好,法里德。不,它没有。 Firestore 对这个问题的参与只是为了获取数据。我的主要问题是如何根据匹配的 ID 将检索到的数据推送到现有数组中的特定位置
标签: arrays angular google-cloud-firestore