Home > database >  Take value from object if key exists otherwise take value
Take value from object if key exists otherwise take value

Time:12-13

Is there an operator in javascript that will extract my props value if exists, if props not exist that will extract rowData[key]?

Example: rowData[key].props

if props exists = rowData[key].props

if props not exists = rowData[key]

CodePudding user response:

null coalesce will do this

const result = rowData[key].props ?? rowData[key];

CodePudding user response:

You can use ternary operator

let value_you_want = rowData[key]?.props ? rowData[key].props : rowData[key]

  • Related