I am trying to map through an array to complete an async operation but what I’m getting back is the promise pending, not the actual object.
const movies = [];
let queryString =
"https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=###&page=";
for (let i = 1; i < 11; i++) {
movies.push({ number: i, query: queryString + i });
}
Basically I’m trying to get an array of objects with 200 items. As you can see in the image above every each array of has 20 movie objects, I want to concatenate all those movies objects into one final array.
const result = [{...}]
result.length // output: 200
Considering that the length of my allMovies array could vary or might be a bit to long for me to hardcode everything they I did it.
const allMovies = [];
let queryString =
"https://api.themoviedb.org/3/discover/movie?sort_by=popularity.desc&api_key=###&page=";
for (let i = 1; i < 11; i++) {
movies.push({ number: i, query: queryString + i });
}
This is exactly what I was looking for, thanks a lot dude. Flat is a lot cleaner and it’s only not supported in IE, which I don’t care too much honestly, still I think I’ll go with the second method. Would you be so kind to explain the syntax for the second method?
[].concat.apply([], ...)
On a side note, just realized the maximum amount of items in an array is 100? If so, why is that? Everything seems to be concatenated into one array but the console shows the array split in 2 arrays of 100 items each.
There’s no limit to the amounts of items in an array. Maybe it is chrome developer tools trying to make it easier for you to visualize your data?
Apply gets its second argument (the array of array of movies) and calls the function (concat) as if each item in the array was an argument for the function.
So, you can use concat like that: [].concat([4], [5], [2, 3], ...), this is exactly what apply is doing, it’s calling each sub-array as if it was an argument.