0

A third party service returns a massively hierarchical JSON, so to get some data I need to call something like

const itemId = 12345;
const image = data['item'][itemId]['images'][0]

and I would like the image to be an empty string if any of these hierarchical fields are not found, and not an exception that would stop the program. What would be the best / compact way to express it in Typescript?

onkami
  • 8,791
  • 17
  • 90
  • 176
  • 1
    See [optional chaining](https://stackoverflow.com/questions/64573952/how-does-the-javascript-optional-chaining-operator-works) – trincot Mar 08 '22 at 15:20
  • 1
    `data?.['item']?.[itemId]?.['images']?.[0] ?? ''` – Ivar Mar 08 '22 at 15:21

0 Answers0