In my array of objects, each message is connected to either a user, a customer, or a client through a One To Many relationship:
What I want to explore is the possibility of using a variable to make the code dynamic without violating the DRY principle.
The example provided is a console.log, but what is actually needed is a link that only requires the ID of the messages related to a specific item in order to display other relevant data.
let messages = [{
"id": 1,
"title": "quidem molestiae enim",
"user": {
id: 4,
name: 'Alex',
surnme: 'Rossowell'
},
"customer": null,
"client": null
},
{
"id": 2,
"title": "sunt qui excepturi placeat culpa",
"client": {
id: 7,
name: 'Andreah',
surnme: 'Carlos'
},
"customer": null,
"user": null
},
{
"id": 3,
"title": "omnis laborum odio",
"customer": {
id: 2,
name: 'Maddie',
surnme: 'Cucurew'
},
"user": null,
"client": null
},
{
"id": 4,
"title": "non esse culpa molestiae omnis sed optio",
"user": {
id: 5,
name: 'Sophia',
surnme: 'Riccon'
},
"customer": null,
"client": null
}
]
messages.map(mess => {
mess.user && console.log(mess.user.id);
mess.customer && console.log(mess.customer.id);
mess.client && console.log(mess.client.id);
})
The URL structure will be as follows:
If related to user: http:\....data\id_user
If related to client: http:\....data\id_client
If related to customer: http:\....data\id_customer
Any help on this matter would be greatly appreciated.