ghost_of_the_code
ghost_of_the_code

Reputation: 349

How can I dynamically recall the item associated with a One to Many relationship?

I have an array of objects where each message is related to a user, or to a customer, or to a client, through a One To Many relationship:

What I would like to understand is whether it is possible, without repeating the code, therefore without violating the DRY principle, to use a variable to make everything dynamic.

The following example is a console.log, but obviously what is needed is not this, but a link that takes only the ID of those messages related to a specific item to print the other corresponding 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 will be like this:

if related to user: http:\....data\id_user

if related to client: http:\....data\id_client

if related to customer: http:\....data\id_customer

Can anyone kindly help me?

Upvotes: 1

Views: 56

Answers (1)

isherwood
isherwood

Reputation: 61036

I might use optional chaining to detect a populated property.

const id = mess.user?.id || mess.customer?.id || mess.client?.id;

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
  },
  {
    "id": 5,
    "title": "non esse culpa molestiae omnis sed optio",
    "customer": null,
    "client": null
  }
]

messages.map(mess => {
  console.log('Message: ', mess.id);
  const id = mess.user?.id || mess.customer?.id || mess.client?.id;
  console.log('Id: ', id ? id : 'not found');
})

Upvotes: 1

Related Questions