Created
May 4, 2021 09:39
-
-
Save ozknozsrt/078d76a7c26b807d16e0b4119ca0d48e to your computer and use it in GitHub Desktop.
Group objects in Array using reduce
This file contains hidden or 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
let meetingOriginal = [{ | |
id: 19, | |
datetime: '2019-01-02T13:28:03+05:30', | |
status: 'proposed', | |
createdBy: 'buyer', | |
buyerId: 2, | |
buyerFirstName: 'Demo', | |
buyerLastName: 'User', | |
buyerProfileUrl: null, | |
petId: 1, | |
petName: 'Penny', | |
petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg', | |
ownerFirstName: 'Faizan', | |
ownerLastName: 'Saiyed', | |
ownerProfileUrl: null, | |
ownerId: 3, | |
}, { | |
id: 20, | |
datetime: '2019-01-03T13:28:06+05:30', | |
status: 'proposed', | |
createdBy: 'buyer', | |
buyerId: 2, | |
buyerFirstName: 'Demo', | |
buyerLastName: 'User', | |
buyerProfileUrl: null, | |
petId: 1, | |
petName: 'Penny', | |
petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg', | |
ownerFirstName: 'Faizan', | |
ownerLastName: 'Saiyed', | |
ownerProfileUrl: null, | |
ownerId: 3, | |
}, { | |
id: 21, | |
datetime: '2019-01-04T13:28:09+05:30', | |
status: 'proposed', | |
createdBy: 'buyer', | |
buyerId: 2, | |
buyerFirstName: 'Demo', | |
buyerLastName: 'User', | |
buyerProfileUrl: null, | |
petId: 1, | |
petName: 'Penny', | |
petMedia: 'https://s3.us-east-2.amazonaws.com/s3-petquest-dev/images/1546324490530.jpg', | |
ownerFirstName: 'Faizan', | |
ownerLastName: 'Saiyed', | |
ownerProfileUrl: null, | |
ownerId: 3, | |
}, { | |
id: 22, | |
datetime: '2019-01-04T13:28:09+05:30', | |
status: 'proposed', | |
createdBy: 'buyer', | |
buyerId: 3, | |
buyerFirstName: 'Demo', | |
buyerLastName: 'User', | |
buyerProfileUrl: null, | |
petId: 2, | |
petName: 'Jerry', | |
petMedia: 'https://static1.squarespace.com/static/52784cdde4b07cdbb003018f/t/56259d40e4b043d43a71cc73/1445305665196/Cavapoo-dog-painting-square-a.jpg?format=500w', | |
ownerFirstName: 'Jony', | |
ownerLastName: 'Snowy', | |
ownerProfileUrl: null, | |
ownerId: 5, | |
}]; | |
let op = meetingOriginal.reduce((o,c)=>{ | |
if(o[c.buyerId]){ | |
o[c.buyerId]['meetings'].push({ | |
id: c.id, datetime: c.dateTime, status: c.status, createdBy: c.createdBy | |
}) | |
} else { | |
let { buyerId,buyerFirstName,buyerLastName,buyerProfileUrl,petId,petName,petMedia,ownerFirstName,ownerLastName,ownerId,ownerProfileUrl} = c | |
o[c.buyerId]={buyerId: buyerId, buyerFirstName:buyerFirstName, buyerLastName:buyerLastName, buyerProfileUrl:buyerProfileUrl, petId:petId, petName:petName, petMedia:petMedia, ownerFirstName:ownerFirstName, ownerLastName:ownerLastName, ownerId:ownerId, ownerProfileUrl:ownerProfileUrl, meetings : [{ datetime: c.dateTime, status: c.status, createdBy: c.createdBy }]}} | |
return o; | |
},{}) | |
let finalOp = Object.values(op); | |
console.log(finalOp) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment