Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

This question already has an answer here:

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?

share|improve this question

marked as duplicate by Günter Zöchbauer angular2 Oct 19 '16 at 5:09

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

up vote 1 down vote accepted

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.

share|improve this answer
    
thank you. it works :) – Brano_SR Mar 8 '16 at 15:11
    
Thanks @MarkRajcok I'm prone to mix f and v in words like "life" and "safe". Thanks for fixing! – Günter Zöchbauer Mar 8 '16 at 18:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.