Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement