fetch("https://www.breakingbadapi.com/api/episodes")
.then((response) => {
return response.json()
})
.then((data) => { return data })
.then((data) => {
for (const ele of data) {
// First select Breaking Bad series
// Because the API Has "Better Call Saul as well"
if (ele.series === "Breaking Bad") {
// Select all seasons to append them to a dropdown menu
if (ele.season != null) {
let season = ele.season;
// console.log(season)
return season;
}
}
}
})
I want to create an array of all seasons from all objects, and then extract duplicate seasons, to append them to the DOM
Change the console.log you’ve got to a push() call on the array from step 1
After the loop, return the array from step 1
Buuuut if I were writing this code then I would use a combination of array methods which I find more readable.
fetch("https://www.breakingbadapi.com/api/episodes")
.then((res) => res.json())
.then((episodes) => {
// Take the array of episodes
const seasons = episodes
// and only keep the Breaking Bad episodes
.filter((episode) => episode.series === "Breaking Bad")
// and only keep the episodes with an affiliated season
.filter((episode) => episode.season)
// and convert those episode objects to their season value
.map((episode) => episode.season);
// Do stuff with the seasons array
});
By the way, the simplest way to do deduplication I’ve seen is to throw the array into a Set, and then get it back as an array via spread (this requires ES6).