rxjs .pipe()添加一些项目到结果数组

发布于 2025-02-10 17:22:59 字数 589 浏览 2 评论 0原文

我需要在从阿波罗获得的结果数组中添加一些静态值。这是我的代码过滤数组并按名称按字母顺序排列,这就是为什么我想在这2个管道之间添加项目:

return this.myService
  .loadCards()
  .pipe(
    map((allCards) =>
      allCards.filter((card) =>
        card.name.toLowerCase().includes(searchQuery.toLowerCase()),
      ),
    ),
  )
  .pipe( // Insert 3 hardcoded objects here)
  .pipe(
    map((allCards) =>
      allCards.sort((a, b) => {
        if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
        if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
        return 0;
      }),
    ),
  );

I have a need of adding some static values to the result array that i get from apollo. Here is my code that filters the array and sorts it alphabetically by name, that is why i want to add items in between those 2 pipes:

return this.myService
  .loadCards()
  .pipe(
    map((allCards) =>
      allCards.filter((card) =>
        card.name.toLowerCase().includes(searchQuery.toLowerCase()),
      ),
    ),
  )
  .pipe( // Insert 3 hardcoded objects here)
  .pipe(
    map((allCards) =>
      allCards.sort((a, b) => {
        if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
        if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
        return 0;
      }),
    ),
  );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白馒头 2025-02-17 17:22:59

您可以加入两个阵列,例如[... arrayone,... arraytwo],因此请尝试(您可以在额外的地图中移动它,但不必这样做):

return this.myService
   .loadCards()
   .pipe(map((allCards) =>{
      return [
        allCards.filter((card) =>
            card.name.toLowerCase().includes(searchQuery.toLowerCase()),
        ),
        ...["myelement"]
       ];    
    })
).pipe(map((allCards) =>
  allCards.sort((a, b) => {
    if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
    if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
    return 0;
   }),
  ),
);

you can concatenate two arrays like [...arrayone,...arraytwo], so try this (you can move it in an extra map but you don't have to):

return this.myService
   .loadCards()
   .pipe(map((allCards) =>{
      return [
        allCards.filter((card) =>
            card.name.toLowerCase().includes(searchQuery.toLowerCase()),
        ),
        ...["myelement"]
       ];    
    })
).pipe(map((allCards) =>
  allCards.sort((a, b) => {
    if (a.name.toLowerCase() < b.name.toLowerCase()) return -1;
    if (a.name.toLowerCase() > b.name.toLowerCase()) return 1;
    return 0;
   }),
  ),
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文