Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to implement a function to create an object?

There is object:

const filters = ref<filterType>({
  date: {
    value: '',
  },
  user: {
    value: '',
  },
  userId: {
    value: '',
  },
...

There is a function for sending data, to which an object is passed as a parameter based on filters’s ref:

({ 
  date: filters.value.date.value || undefined,
  user: filters.value.user.value || undefined,
  userId: filters.value.userId.value || undefined,
  ... 
})

Then I need the same object that but in different function. In order not to duplicate two large objects, I probably need to implement a function that will create this object. How to implement it correctly?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can create and export a function that returns the object based on filters:

export const getFilterParams = (filters: filterType) => ({
  date: filters.value.date.value || undefined,
  user: filters.value.user.value || undefined,
  userId: filters.value.userId.value || undefined,
  ...
});

You would call it like that:

const params = getFilterParams(filters.value);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading