AmerllicA
AmerllicA

Reputation: 32512

Ternary condition in ES6 destructuring object

I wrote a stateless function, in this function I use a destructuring object declaration, but one of my variables has conditions. I wrote it with a ternary condition. but I cannot declare it in the destructuring assignment structure.

This is my declaration:

const {
  data: { result: { total: total } = {} },
  tags: { result: { categoryFilter: { Title: title } = {} } = {} }
} = props;

const pageNo = props.filters.pageno
  ? props.filters.pageno - 1
  : 0;

Upvotes: 4

Views: 7890

Answers (2)

AmerllicA
AmerllicA

Reputation: 32512

const {
  data: { result: { total: total = 0 } = {} } = {},
  filters: { pageno: TempPageNo = 0 } = {},
  tags: {
    result: { categoryFilter: { Title: title = "something" } = {} } = {}
  } = {}
} = props;

const pageno = TempPageNo ? TempPageNo - 1 : TempPageNo;

Upvotes: -2

TPReal
TPReal

Reputation: 1585

You cannot directly. You can do:

const {filters: {pageno}} = props;
const realPageno = pageno ? pageno - 1 : 0;

Upvotes: 4

Related Questions