Transforming Observable with .map

Viewed 33023

I have a problem with transforming my observable. Details below:

I have a data like this

[
    {
      'firstName': 'John',
      'lastName': 'Cash',
      'age': 20
    }
  ];

Then I get this data from api:

  public getData(): Observable<Data[]> {
    return this.http.get('xxx')
    .map(
      response => response.json()
    );
  }

Then, I'm trying to subscribe this:

this.service.getData.subscribe(
        (res) => this.data = res
      );

And It's ok, it's working. But I need to modify the structure of object and I would like to use .map to transform received object to this schema:

[
    {
      'firstName': 'John',
      'lastName': 'Cash',
      'age': 20,
'newProperty': 'value'
    }
  ];

.. and nothing working for me.. :/ Even if I don't want to add new property, but modify a value on for example firstName:

  .map(
    return x => x[0].firstName = 'asd'
  )

it's not working (Type 'string' is not assignable to type 'Data[]', I know what it means, but I don't know how to do that, where is my mistake?)

2 Answers
Related