-
Notifications
You must be signed in to change notification settings - Fork 0
/
items.js
55 lines (50 loc) · 1.45 KB
/
items.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
const posts = [];
let imageIndex = 0;
const imageArray = [];
const apiKey = 'WLcQFJMXO_PBQfpVFZ1jisJWFvQZRm3wpjBfPWxxMZ0';
const url = `https://api.unsplash.com/photos?client_id=${apiKey}`;
// this dodesn't work because the code block that creates and pushes
// the 'item' objects to the 'posts' array is running before the 'imageArray'
// is populated by the fetch request.
// fetch(url)
// .then((response) => response.json())
// .then((data) => {
// for (let i = 0; i < 10; i++) {
// imageArray.push(data[i].urls.small);
// }
// console.log(posts);
// console.log(imageArray);
// // console.log(images);
// })
// .catch((error) => console.log(error));
// for (let i = 0; i < 100; i++) {
// let item = {
// id: i,
// title: `Post ${i}`,
// image: imageArray[imageIndex]
// };
// posts.push(item);
// imageIndex++;
// if (imageIndex > imageArray.length - 1) imageIndex = 0;
// }
fetch(url)
.then((response) => response.json())
.then((data) => {
for (let i = 0; i < 10; i++) {
imageArray.push(data[i].urls.small);
}
//console.log(posts);
// console.log(imageArray);
for (let i = 0; i < 100; i++) {
let item = {
id: i,
title: `Post ${i}`,
image: imageArray[imageIndex]
};
posts.push(item);
imageIndex++;
if (imageIndex > imageArray.length - 1) imageIndex = 0;
}
console.log(posts);
})
.catch((error) => console.log(error));