Skip to content

Instantly share code, notes, and snippets.

@awolad
Created March 20, 2019 15:10
Show Gist options
  • Save awolad/9953b3174c2a0923c524a34c02aa67ed to your computer and use it in GitHub Desktop.
Save awolad/9953b3174c2a0923c524a34c02aa67ed to your computer and use it in GitHub Desktop.
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