0

I Have two objects with different structure, trying to merge this two objects with matched data based on a key value, and make a new object with complete data from object1 and object2.

my Object1

{  
   "schedule":{  
      "onward":{  
         "journey":[  
            {  
               "trips":[  
                  {  
                     "origin":{  
                        "airportCode":"AMS"
                     },
                     "destination":{  
                        "airportCode":"BCN"
                     },
                     "FlightNumber":"KL1665"
                  }
               ]
            },
            {  
               "trips":[  
                  {  
                     "origin":{  
                        "airportCode":"AMS"
                     },
                     "destination":{  
                        "airportCode":"BCN"
                     },
                     "FlightNumber":"MF9343"
                  }
               ]
            }
         ]
      }
   }
}

object 2

{  
   "flights":[  
      {  
         "flightNumber":"KL1665",
         "price":223,
         "available":10
      },
      {  
         "flightNumber":"KL112",
         "price":223,
         "available":10
      },
      {  
         "flightNumber":" KL112",
         "price":223,
         "available":10
      }
   ]
}

i Would like to merge "price" and "available" to object which matches with "flightNumber" and create a new object with all data.

I have tried to use loadash and underscore but couldnt figure it. Help would be great, Thank in Advance.

4
  • final expected object {"schedules": {"onwardSchedules": {"trips": [{"flights": [{"origin": {"airportCode": "AMS"}, "destination": {"airportCode": "BCN"}, "marketingFlightNumber": "KL1665","price":223, "available": 10 }] }, {"flights": [{ "origin": { "airportCode": "AMS"}, "destination": {"airportCode": "BCN" } , "marketingFlightNumber": "MF9343" }] }] } } } Commented Oct 3, 2016 at 17:30
  • you just need to do this manually. put the data from flights you want into schedule. There isn't a "short cut" for something this specific. Commented Oct 3, 2016 at 17:39
  • @bryan60 i get this data dynamically from Ajax so manual update is not possible. Commented Oct 3, 2016 at 17:42
  • I mean you need to write the code to combine these manually with loops. There isn't a plug in solution from something like lodash or underscore for something like this. Commented Oct 3, 2016 at 17:43

1 Answer 1

2

2 simple forEach will do it:-

flights.flights.forEach(function(e, i) {
  schedule.schedule.onward.journey.forEach(function(e2, i2) {
    if (e.flightNumber == e2.trips[0].FlightNumber) {
      e2.trips[0] = $.extend(e, e2.trips[0]);
    }
  });
});

Working example:

var schedule = {
  "schedule": {
    "onward": {
      "journey": [{
        "trips": [{
          "origin": {
            "airportCode": "AMS"
          },
          "destination": {
            "airportCode": "BCN"
          },
          "FlightNumber": "KL1665"
        }]
      }, {
        "trips": [{
          "origin": {
            "airportCode": "AMS"
          },
          "destination": {
            "airportCode": "BCN"
          },
          "FlightNumber": "MF9343"
        }]
      }]
    }
  }
};

var flights = {
  "flights": [{
      "flightNumber": "KL1665",
      "price": 223,
      "available": 10
    }, {
      "flightNumber": "KL112",
      "price": 223,
      "available": 10
    }, {
      "flightNumber": " KL112",
      "price": 223,
      "available": 10
    }

  ]
};

flights.flights.forEach(function(e, i) {
  schedule.schedule.onward.journey.forEach(function(e2, i2) {
    if (e.flightNumber == e2.trips[0].FlightNumber) {
      e2.trips[0] = $.extend(e, e2.trips[0]);
    }
  });
});

console.log(schedule);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You so much for the reply, Can you please suggest a way in which i can get the final object in the same structure of Object 1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.