Can someone help me with avoiding errors in TypeScript when filtering a specific variable in JSON data? Here is an example of part of my JSON data:
Containing multimedia details:
"per_facet": [],
"geo_facet": [],
"multimedia": [
{
"url": "https://static01.nyt.com/images/2016/09/07/world/06NYTNow-Obama2/06NYTNow-Obama2-thumbStandard-v2.jpg",
"format": "Standard Thumbnail"
},
{
"url": "https://static01.nyt.com/images/2016/09/07/world/06NYTNow-Obama2/06NYTNow-Obama2-thumbLarge-v2.jpg",
"format": "thumbLarge",
}]
There is also an element without any multimedia content:
"per_facet": [],
"geo_facet": [],
"multimedia": [],
As you can see, the "multimedia" variable in the array has internal values in one of the elements but not in the other.
This is how I currently try to retrieve the URL parameter (which is an image):
<img [src]= "item.multimedia[0].url" *ngIf="item.multimedia[0].url.indexOf('http') === 0">
When trying to access the URL parameter of the multimedia array, I get an error saying "Cannot read URL of undefined". This makes sense since there is no data present to display. This method works for JavaScript, but I'm unsure how to solve this in TypeScript.
Does anyone know how to avoid this error and successfully retrieve other parameters from this array?
Thank you in advance. Suresh