Ionic 2:Http.get() 运行 函数用于响应结果中每一行的 Rx

Ionic 2: Http.get() run function for each row in response results with Rx

Q) 我如何为这次调用中结果的每一行 运行 一个函数(例如:将每个项目的 ID 添加到标题末尾的函数)?

假设我在下面的回复是这样的:

[
  { id: 1. title: 'first', completed: 1 },
  { id: 2. title: 'second', completed: 1 },
  { id: 3. title: 'third', completed: 1 }
]

我的 getTodos 代码:

return this._http.get(url, {
  headers: headers
}).map(res => <Todo[]>res.json());

我的待办界面:

export interface Todo {
  id: number;
  title: string;
  completed: boolean;
}

我想在每个响应行上 运行 我的示例函数:

function addToTitle(item) {
  item.title += " " + item.id;
}

您可以使用数组的 map 函数添加另一个 map 运算符:

return this._http.get(url, {
  headers: headers
}).map(res => <Todo[]>res.json())
.map((todos) => {
  todos.forEach((todo) => {
    todo.title += ' ' + todo.id;
  });
  return todos;
});