SHARE
TWEET

Untitled

a guest Mar 25th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var array1 = [{
  2.   agentID: 28,
  3.   bookingDates: ["2019-02-01,2019-03-04"],
  4.   roomNo: [101],
  5.   roomID: [1]
  6. }, {
  7.   agentID: 29,
  8.   bookingDates: ["2019-02-01,2019-03-02"],
  9.   roomNo: [301],
  10.   roomID: [3]
  11. }, {
  12.   agentID: 28,
  13.   bookingDates: ["2019-02-01,2019-03-03"],
  14.   roomNo: [102],
  15.   roomID: [2]
  16. }];
  17.  
  18. var output = [];
  19.  
  20. array1.forEach(function(item) {
  21.   var existing = output.filter(function(v, i) {
  22.     return v.agentID == item.agentID;
  23.   });
  24.   if (existing.length) {
  25.     var existingIndex = output.indexOf(existing[0]);
  26.     output[existingIndex].roomID = output[existingIndex].roomID.concat(item.roomID);
  27.     output[existingIndex].roomNo = output[existingIndex].roomNo.concat(item.roomNo);
  28.     output[existingIndex].bookingDates = output[existingIndex].bookingDates.concat(item.bookingDates);
  29.   } else {
  30.     if (typeof item.roomNo == 'string')
  31.       item.bookingDates = [item.bookingDates];
  32.     output.push(item);
  33.   }
  34. });
  35.  
  36. console.log(output);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top