-
Notifications
You must be signed in to change notification settings - Fork 0
/
index2.js
29 lines (23 loc) · 1018 Bytes
/
index2.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
// Declaring the two objects
let myObj = {title:'Post 1', body:'This is post 1'};
let myObj2 = { title: 'Post 2', body: 'This is post 2' };
let myObj3 = { title: 'Post 3', body: 'This is post 3' };
// With "let," like there is here, you have the option of
// block scope or global scope. With "var," you only have
// global scope option.
// This has "let" because you want to avoid using "var" as
// much as possible because it's a global variable, whereas
// let is a block-level scope.
myObj.title
// Make an array of the objects
let myArray = [myObj, myObj2, myObj3];
// Iterate through each object in the array
window.addEventListener('load', () => { // <------ This is WAITING for the HTML content to
console.log(myObj.title)
for (let i = 0; i < myArray.length; i++) { // load BEFORE loading JavaScript.
//console.log(myArray[i]);
document.getElementById('container').innerHTML += '<h1>' + myArray[i] + '</h1>'
}
});
// Print the objects out in the browser
console.log(myArray);