Accessing Data in an Array of Objects
.filter() & For Loop methods
To begin, we are going to write code for an array containing several objects:
The first method we are going to discuss the the .filter method. Lets say we want to return all anime shows that are one season long. We would have to loop through the array and return all objects that have a seasons value that is equal to one. A simple way to do this would be using the .filter() method. This method creates an array with all array elements based on a given specification. In this case, we would want to specify seasons deeply equals 1:
This function returns:
But now, lets say you only want to only return the title of the anime shows that have only one season. This would require a For Loop. We first must use a For Loop to iterate through the anime array. Then we need to return the title of the object that contain seasons with a value of 1.
This code would appear as follows: