zemmsoares
zemmsoares

Reputation: 353

If property does not exist

I am filtering the data through arr.map() and then sendig that data to a React-Table, since my API returns several properties which i will not be using for now.

const filteredPuntedIssues = this.state.puntedIssues.map(item => ({
  assignee: item ? item.assigneeName : 'Empty',
  id: item ? item.id : 'Empty',
  key: item ? item.key : 'Empty',
  type: item ? item.typeName : 'Empty',
  summary: item ? item.summary : 'Empty',
  storyPoints: item.estimateStatistic ? item.estimateStatistic.statFieldValue.value : '0',
}));

This works if any of the values are null, but if somewhere in my API one of those properties dont exist, it turns out to be empty, and i want to make it a string ex: "Does not Exists"

Upvotes: 1

Views: 167

Answers (2)

GauthierG
GauthierG

Reputation: 308

You can add another ternary condition for that. For example :

assignee: item ? (item.assigneeName ? item.assigneeName : "Does not Exists") : 'Empty'

ou

assignee: item ? (item.assigneeName || "Does not Exists") : 'Empty'

Upvotes: 1

Jonas Wilms
Jonas Wilms

Reputation: 138267

 const filteredPuntedIssues = this.state.puntedIssues.map(item => 
   ["assigneeName", "id", "key", "typeName", "summary", "storyPoints"].every(k => item[k])
   ? item
   : "Does not exist"
);

Upvotes: 1

Related Questions