Currently working on a ternary statement to display the certification of a movie (e.g. PG, R...).
If the array length is zero or undefined, I'm aiming to output an error message stating "No Certification Available For This Movie". While I have successfully managed to display the error message when the array is empty, I'm facing a challenge in handling undefined arrays. I constantly receive a console error reading
TypeError: Cannot read properties of undefined (reading 'release_dates')
.
Below is my attempt so far:
const movieRating = rating.results;
//Fetching US ratings
const findUSRating = movieRating.find((item) => item.iso_3166_1 === 'US');
//Applying filter to remove null certifications from US ratings.
const array = findUSRating.release_dates.filter((item) => item.certification);
//Ternary statement & my current hurdle.
console.log(array.length > 0 || array[index] !== undefined ? array[0].certification : `No Certification Available For This Movie`);