通过迭代可观察对象来创建对象数组

Create array of objects by iterating through an observable

我有一个 Angular 组件,它订阅了一个由 Angular 服务返回的可观察对象。

组件: help.component.ts

import { WikiService } from '../../../services/wiki.service';

import { WikiTree } from '../../../interfaces/WikiTree';

export class HelpComponent implements OnInit {

    wikiTree$: Observable<WikiTree>

    public infoCards: Array<Object>;

    constructor(private wikiService: WikiService) {}

    ngOnInit() {
            this.wikiTree$ = this.wikiService.GetWikiHierarchy();
            this.wikiTree$.subscribe(()=>{
               //TODO: Create infoCards array by iterating through the results array inside the wikiTree observable.
            }
        }
}

wikiTree$ Observable 具有以下JSON,转换为 TypeScript:

{
    "page": {
        "results": [
            {
                "id": "123456789",
                "type": "page",
                "status": "current",
                "title": "Start here",
                "extensions": {
                    "position": 0
                },
                "_links": {
                    "webui": "/display/MYSPACE/Start+here",
                    "edit": "/pages/resumedraft.action?draftId=123456789",
                    "tinyui": "/x/BQD2Mw",
                    "self": "https://wiki.abc.com/rest/api/content/123456789"
                },
                "_expandable": {
                    "container": "/rest/api/space/MYSPACE",
                    "metadata": "",
                    "operations": "",
                    "children": "/rest/api/content/123456789/child",
                    "restrictions": "/rest/api/content/123456789/restriction/byOperation",
                    "history": "/rest/api/content/123456789/history",
                    "ancestors": "",
                    "body": "",
                    "version": "",
                    "descendants": "/rest/api/content/123456789/descendant",
                    "space": "/rest/api/space/MYSPACE"
                }
            },
            {
                "id": "567890123",
                "type": "page",
                "status": "current",
                "title": "FAQ",
                "extensions": {
                    "position": 1
                },
                "_links": {
                    "webui": "/display/MYSPACE/FAQ",
                    "edit": "/pages/resumedraft.action?draftId=567890123",
                    "tinyui": "/x/HQD2Mw",
                    "self": "https://wiki.abc.com/rest/api/content/567890123"
                },
                "_expandable": {
                    "container": "/rest/api/space/MYSPACE",
                    "metadata": "",
                    "operations": "",
                    "children": "/rest/api/content/567890123/child",
                    "restrictions": "/rest/api/content/567890123/restriction/byOperation",
                    "history": "/rest/api/content/567890123/history",
                    "ancestors": "",
                    "body": "",
                    "version": "",
                    "descendants": "/rest/api/content/567890123/descendant",
                    "space": "/rest/api/space/MYSPACE"
                }
            }
        ],
        "start": 0,
        "limit": 25,
        "size": 2,
        "_links": {
            "self": "https://wiki.abc.com/rest/api/content/998877665/child/page"
        }
    },
    "_links": {
        "base": "https://wiki.abc.com",
        "context": "",
        "self": "https://wiki.abc.com/rest/api/content/998877665/child"
    },
    "_expandable": {
        "attachment": "/rest/api/content/998877665/child/attachment",
        "comment": "/rest/api/content/998877665/child/comment"
    }
}

打字稿:WikiTree.ts

export interface WikiTree {
    page: Page;
    _links: Links;
    _expandable: Expandable;
  }
  export interface Page {
    results?: (ResultsEntity)[] | null;
    start: number;
    limit: number;
    size: number;
    _links: Links1;
  }
  export interface ResultsEntity {
    id: string;
    type: string;
    status: string;
    title: string;
    extensions: Extensions;
    _links: Links2;
    _expandable: Expandable1;
  }
  export interface Extensions {
    position: number;
  }
  export interface Links2 {
    webui: string;
    edit: string;
    tinyui: string;
    self: string;
  }
  export interface Expandable1 {
    container: string;
    metadata: string;
    operations: string;
    children: string;
    restrictions: string;
    history: string;
    ancestors: string;
    body: string;
    version: string;
    descendants: string;
    space: string;
  }
  export interface Links1 {
    self: string;
  }
  export interface Links {
    base: string;
    context: string;
    self: string;
  }
  export interface Expandable {
    attachment: string;
    comment: string;
  }

我想遍历 "page" -> "results" array in the wikiTree$ observable in the subscribe method and create an array of objects called infoCards, 作为使用此 JSON 在组件中定义,其中一些值来自 wikiTree$ observable。 :

[{
        "title": "Start here",
        "titleLink": "/page/wiki/123456789",
        "children": []
    },
    {
        "title": "FAQ",
        "titleLink": "/page/wiki/567890123",
        "children": []
    }
    ]

我该怎么做?

更新:

InfoCard 组件

import { Component, OnInit, Input } from '@angular/core';

@Component({
  selector: 'app-info-card',
  templateUrl: './info-card.component.html',
  styleUrls: ['./info-card.component.css']
})
export class InfoCardComponent implements OnInit {
  @Input('infoCard') infoCard: Array<Object>;

  constructor() { }

  ngOnInit() {
  }

}

如果我要面对这个问题,我会像下面这样解决这个问题:

首先,我会创建一个 Model (Class),就像您 WikiTree.ts 中的模型一样对于信息卡。我将其设为 Class 以在订阅方法中创建它的 Object -

export class InfoCard{
        title: string;
        titleLink: string;
        children: string;
  }

之后在 组件中:help.component.ts 我会这样声明信息卡 -

infoCards: InfoCard[]=[];

显然,您必须从 WikiTree.ts-

导入 InfoCard
import { InfoCard} from '../../../interfaces/WikiTree';

然后在 subscribe 方法中,我会将数据映射为 Observable-

    this.wikiTree$.subscribe(data =>{
      data.page.results.map(
       result=>{
          let newInfoCard = new InfoCard();
          newInfoCard.title = result.title;
          newInfoCard.children= result._expandable.children;
          newInfoCard.titleLink= result._links.self;
          this.infoCards = [this.infoCards, ...newInfoCard]
        }
       )
     }

因为你已经添加了组件部分,所以你也可以试试这个-

初始化数组-

infoCards: Array<Object>= new Array();

可能有更多的选择来完成它。我正在分享我的。如果您有任何不明白或有疑问或我犯的错误,请告诉我。

    forkJoin(
        this.wikiService.GetWikiHierarchy(),
        this.wikiService.GetWikiChildren()
    ).subscribe(([data, children]) => {
        data.page.results.map(
            result => {
                this.infoCards.push({
                    'title': result.title,
                    'titleLink': result._links.self,
                    'children': children.whatEver
                });
            }
        )
    });

而且,如果 children 调用取决于之前的服务调用,您可以尝试这样的操作 -

  this.wikiService.GetWikiHierarchy().subscribe(
    data=>{
      data.page.results.map(
        result=>{
          this.wikiService.GetWikiChildren(result.id).subscribe(
            child=>{
                this.infoCards.push({
                  'title': result.title,
                  'titleLink': result._links.self,
                  'children': child.whatEver
                    });
            }
          )
        }
      )
    }
  )

以下订阅将在新数据进入时根据您提供的结构创建新对象。我假设您只需要指定格式的 title 和 titleLink 并且 children 数组是从另一个服务派生的?

this.wikiTree$.subscribe( res => {
    const newInfoCards = res.page.results.map(result => ({
        "title": result.title,
        "titleLink": "/page/wiki/"+ result._links.self.split('/').pop(),
        "children": []
    }),
    this.infoCards.concat(newInfoCards);
})