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?

>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);

Leave a Reply