Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.92 KB | None | 0 0
  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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement