angular - 如何在可观察的情况下查找并将项目移动到集合顶部
问题描述
Rxjs 新手。我熟悉管道、过滤器、排序、地图等,但无法解决这个问题。我需要在我的可观察返回数据中找到一个项目,并将其移动到顶部,然后再显示。在此示例中,Darth Vader 是第 4 项,我想找到并移动到可观察数组/集合中的第 0 个或第一个位置。 https://stackblitz.com/edit/findandmakefirst
解决方案
import { People } from './sw-people.model';
interface Results {
count: Number,
next: String,
previous: string,
results: People[],
}
@Injectable()
export class SwPeopleService {
people$ = this.http.get('https://swapi.co/api/people/')
.map((res: Results) => {
return res.results.sort((a: People, b: People) => {
return a.name === "Darth Vader" ? -1 : b.name === "Darth Vader" ? 1 : 0;
});
});
constructor(private http: HttpClient) {}
}
推荐阅读
- firebase - SwiftUI:.onappear 未在第一个选项卡中显示来自 firebase 的数据
- scala - 我的 fs2 scala 程序没有按预期执行
- c - 在 Docker Alpine 中编译 NSS 模块 - 致命错误:nss.h:没有这样的文件或目录
- r - 如何在 R 中的公式中写使用 quos?
- python - 设置数据集的随机种子以始终选择相同的图像/皮尔逊相关性
- markdown - 向 MARP 演示文稿添加内部链接
- mysql - 通过多个键合并两个 MySQL 表
- django - 使用模型表单并从多个字段中选择多个项目
- python-3.x - 如何简化重复的算术函数?
- spacy - Spacy(短语)匹配多个属性