Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine two object in RxJS

I am working with rxjs and angular 2 inside of a service. I have some json that I was able to access with a get request.

   private _campInfoUrl = 'api/campInfo/campInfo.json';
  constructor(private _http: Http) { }

  getAvailableCamps() {  
    return this._http.get(this._campInfoUrl)
      .map((response: Response) => response.json())

At this point I have all of the data. However to get into this object

      {
       "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"}
  ] 
}

this is what i am trying to do

.map((response: Response) => response.json()) // <IProduct[]>
  .map((tripDate) => ({
    reservations: tripDate.reservations,
    newRes: tripDate.search // <-- how to add this to every return object
  }))

What I am struggling to figure out is a way with rxjs on how to get "search" inside of reservations array of objects like so

"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
 }

In the example above I have the search object added to each index of the reservations array.

I was hoping it was a matter of concatenating or mapping to transform the array. However, I am not able to get inside of each index on the reservations array

Any insight into navigating this json object would be greatly appreciated.

like image 606
Winnemucca Avatar asked Jan 26 '17 07:01

Winnemucca


People also ask

How do you combine two values of objects?

If you want to merge the second object into the first object, instead of creating a new object, you can use Object. assign() . The Object. assign(target, source) function merges the source into the target.

How do you combine properties of two objects?

In the above example, two objects are merged into one using the Object. assign() method. The Object. assign() method returns an object by copying the values of all enumerable properties from one or more source objects.

How do you combine two objects in an array?

Using the spread operator or the concat() method is the most optimal solution. If you are sure that all inputs to merge are arrays, use spread operator . In case you are unsure, use the concat() method. You can use the push() method to merge arrays when you want to change one of the input arrays to merge.


1 Answers

Not sure why you cannot get "inside each index", but you don't have to and you can even do this without RxJS:

.map((response: Response) => response.json()) // <IProduct[]>
  .map((tripDate) => ({
    reservations: tripDate.reservations
      .map(reservation => Object.assign(
        {},
        reservation,
        {
          searchStartDate: tripDate.search.startDate,
          searchEndDate: tripDate.search.endDate
        }
      )
    )
  }))
like image 163
olsn Avatar answered Sep 29 '22 06:09

olsn