Home > Blockchain >  How to set destructured values on properties?
How to set destructured values on properties?

Time:01-05

I have a simple function that returns 2 values, I hoped I could destructure the return of the function into the sort and sortOrder in the filters object, but if I do that I get the error:

Property 'sort' does not exist on type 'DashboardSortingPreferences'.

const { this.filters.sort, this.filters.sortorder } = this.dashboardColumnSortingService.initializeDashboardSortProperties(
    this.user,
    this.dashboardName,
);
const { sorted_column, direction } = this.dashboardColumnSortingService.initializeDashboardSortProperties(
    this.user,
    this.dashboardName,
);

this.filters.sort = sorted_column;
this.filters.sortorder = direction;

This works, and I understand why, but I was wondering if it was possible to set the sort_column and direction directly on the sort and sortOrder properties.

CodePudding user response:

Related answer.

It is possible. On the mdn web docs we can read:

const numbers = [];
const obj = { a: 1, b: 2 };
({ a: numbers[0], b: numbers[1] } = obj);
// The properties `a` and `b` are assigned to properties of `numbers`

so in your case it would look something like this:

({ sorted_column: this.filters.sort, direction: this.filters.sortorder } = 
this.dashboardColumnSortingService.initializeDashboardSortProperties(
this.user,
this.dashboardName));
  • Related