Created
March 20, 2019 15:10
-
-
Save awolad/9953b3174c2a0923c524a34c02aa67ed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var array1 = [{ | |
agentID: 28, | |
bookingDates: ["2019-02-01,2019-03-04"], | |
roomNo: [101], | |
roomID: [1] | |
}, { | |
agentID: 29, | |
bookingDates: ["2019-02-01,2019-03-02"], | |
roomNo: [301], | |
roomID: [3] | |
}, { | |
agentID: 28, | |
bookingDates: ["2019-02-01,2019-03-03"], | |
roomNo: [102], | |
roomID: [2] | |
}]; | |
var output = []; | |
array1.forEach(function(item) { | |
var existing = output.filter(function(v, i) { | |
return v.agentID == item.agentID; | |
}); | |
if (existing.length) { | |
var existingIndex = output.indexOf(existing[0]); | |
output[existingIndex].roomID = output[existingIndex].roomID.concat(item.roomID); | |
output[existingIndex].roomNo = output[existingIndex].roomNo.concat(item.roomNo); | |
output[existingIndex].bookingDates = output[existingIndex].bookingDates.concat(item.bookingDates); | |
} else { | |
if (typeof item.roomNo == 'string') | |
item.bookingDates = [item.bookingDates]; | |
output.push(item); | |
} | |
}); | |
console.log(output); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment