2

I am trying to do something elementary. I have json file pageDefinition.json which is loaded into my component. This is how json looks like:

...
"testArray": [
    {"id": 0, "name": "row1"},
    {"id": 1, "name": "row2"},
    {"id": 2, "name": "row3"}
  ],
...

When I print all array with {{pageDefinition?.testArray | json}} it works. But I want to get only one item with index from array. If I try to get first element: {{pageDefinition?.testArray[0] | json}} app crash with exception:

EXCEPTION: TypeError: Cannot read property '0' of null in [

  {{pageDefinition?.testArray[0] | json}}

   in ExampleFile@10:8]

Where did I make a mistake?

branecko
  • 81
  • 1
  • 10

1 Answers1

2

I think you need to use this longer version

{{pageDefinition?.testArray ? pageDefinition.testArray[0] : null | json}}

The safe navigation variant (i.e., Elvis operator) is only available for . not for [] accessor operators.

Mark Rajcok
  • 362,217
  • 114
  • 495
  • 492
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567