Skip to content
Advertisement

react JavaScript ternary conditional operation

After I import the data as json from the detail page, in ProductDetail > brand > shoes > size.length get the length length is outputting in JSX.

But there is a problem. There are also products without shoes data for each detailed product on the detail page. I want to treat products without data as 0 instead of length as a ternary operator, but I don’t know how to handle it.

JavaScript

But here, data without brand is used using the ternary operator. <p>0</p> : I want to display it like this.

JavaScript

Advertisement

Answer

If you need to show 0 when an object is null or parent object is null, Try some like below

JavaScript

Basically with using optional chaining and || operator, The output will be

JavaScript

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement