Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null checking of array using question mark break

I have this array of object

const a = {a:[{b:[{prop: 10}]}]}

to access the value of 10 I do

a.a[0].b[0].prop // 10

I use this fallback, make sure things doesn't break but it didn't work?

a?.a[0]?.b[0]?.prop

, I can use something like lodash's get or check using the old method but how to use the question mark syntax?

like image 465
Andre Thonas Avatar asked Apr 12 '26 23:04

Andre Thonas


1 Answers

Optional chaining should be written as below

a?.[0]?.b?.[0].prop

like image 188
Hardik Shah Avatar answered Apr 15 '26 13:04

Hardik Shah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!