I am looping through an array of objects in react using array.map. The array takes the form:
const seasons = [
{air_date: null, episode_count: 6},
{air_date: "2020-02-02", episode_count: 6}
]
I am looping through the array using seasons.map, returning JSX if air_date is not null and null otherwise.
seasons.map((season, index) => {
if(season.air_date){
return <span key = {season.id}> {season.episode_count} </span>
}else{
return null; // Is this recommended?
}
})
I have never seen anyone do this (returning null instead of JSX). Is it recommended in react? I don't want to use a for loop.
mapbeing used to loop through arrays. What i have never seen isnullbeing returned instead ofJSX.&&is necessary for your needs for example. You don't even need to use anif/elsestatement like that.