Angular 6:从内部映射JSON

时间:2019-02-28 08:49:27

标签: json angular dictionary

我有这样的代码:

export class ClientSearchService extends QueryService {

  private a = [
    {
      'firstName': 'Danuta',
      'lastName': 'Kowalska',
    },
    {
      'firstName': 'ADAM',
      'lastName': 'Kowalski',
    }
  ];

  private b = {
    'something': [
      {
        'firstName': 'Danuta',
        'lastName': 'Kowalska',
      },
      {
        'firstName': 'ADAM',
        'lastName': 'Kowalski',
      }
    ]
  };


  public list() {

    const testA = of(this.a);
    testA.pipe(
      map((records: Client[]) => records.map((client: Client) => Client.factory(client)))
    ).subscribe(records => {
      console.log(records);
    });

    const testB = of(this.b);
    testB.pipe(
    map(XXXX).subscribe(records => {
       console.log(records);
    });
  }
}

测试(A)是正确的。我在控制台中获得了Client对象数组。

在第二个示例(B)中,我具有包含Clients数据的JSON,但位于“ somethig”键中。 我应该更改什么才能将此JSON映射到Client类?

1 个答案:

答案 0 :(得分:0)

您可以轻松做到:

    const testB = of(this.b.something);