0

Is there a way to simplify the following code?

let { page_size, current_page } = paging;

this.p_start = page_size * current_page;
this.p_current = current_page;
this.p_dg_current = this.p_current + 1;
this.p_size = page_size;

paging is an object and has properties page_size and current_page.

Is there a destructing way to assign the paging.page_size and paging.current_page directly to this.p_size and this.p_current?

  let { this.p_size = page_size, this.p_current = current_page } = paging;

  this.p_start = page_size * current_page;
  this.p_dg_current = this.p_current + 1;
Tom O.
  • 5,730
  • 2
  • 21
  • 35
user21
  • 1,261
  • 5
  • 20
  • 41

3 Answers3

1

Can you try this way

 let { page_size: p_size, current_page: p_current } = paging;
        this.p_start = page_size * current_page;
        this.p_dg_current = this.p_current + 1;
Kesav
  • 151
  • 4
1

The syntax for destructuring is { propertyName: targetExpression = default }. You have the right idea though (and you need to omit the let, as you're not declaring variables):

({ page_size: this.p_size, current_page: this.p_current } = paging);

this.p_start = this.p_size * this.p_current;
this.p_dg_current = this.p_current + 1;
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

Try like this, instead of paging1 use this

let paging = {page_size : 10, current_page : 9};
let paging1 = {p_size : 0, c_size : 0};
({ page_size : paging1.p_size, current_page : paging1.c_size } = paging)

console.log(paging1.p_size+ ' '+ paging1.c_size)
Gaurav Singh
  • 369
  • 1
  • 6