我正在使用服务中的rxjs和angular 2.我有一些json,我可以通过get请求访问.
private _campInfoUrl = 'api/campInfo/campInfo.json'; constructor(private _http: Http) { } getAvailableCamps() { return this._http.get(this._campInfoUrl) .map((response: Response) => response.json())
此时我掌握了所有数据.但要进入这个对象
{ "search": { "startDate": "2016-06-07", "endDate": "2016-06-10" }, "reservations": [ {"campsiteId": 1, "startDate": "2016-06-01", "endDate": "2016-06-04"}, {"campsiteId": 1, "startDate": "2016-06-11", "endDate": "2016-06-13"}, {"campsiteId": 2, "startDate": "2016-06-08", "endDate": "2016-06-09"} ] }
这就是我想要做的
.map((response: Response) => response.json()) //.map((tripDate) => ({ reservations: tripDate.reservations, newRes: tripDate.search // <-- how to add this to every return object }))
我正在努力弄清楚的是rxjs如何在预留数组对象内"搜索"这样的方式
"reservations": [ { "campsiteId": 1, "startDate": "2016-06-01", "endDate": "2016-06-04", "searchStartDate": "2016-06-07, "searchEndDate": "2016-06-10 }, { "campsiteId": 1, "startDate": "2016-06-11", "endDate": "2016-06-13", "searchStartDate": "2016-06-07, "searchEndDate": "2016-06-10 }, { "campsiteId": 2, "startDate": "2016-06-08", "endDate": "2016-06-09", "searchStartDate": "2016-06-07, "searchEndDate": "2016-06-10 }
在上面的示例中,我将搜索对象添加到预留数组的每个索引中.
我希望这是连接或映射转换数组的问题.但是,我无法进入预留数组的每个索引
任何洞察导航这个json对象将不胜感激.
不知道为什么你不能"在每个索引里面",但你不必这样做,你甚至可以在没有RxJS的情况下做到这一点:
.map((response: Response) => response.json()) //.map((tripDate) => ({ reservations: tripDate.reservations .map(reservation => Object.assign( {}, reservation, { searchStartDate: tripDate.search.startDate, searchEndDate: tripDate.search.endDate } ) ) }))