Last active
March 20, 2023 20:11
-
-
Save sandrabosk/8cea9c192cdea263a7a60d653d3dc7ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Data can be organized (structured) in different ways | |
// The following are nested data structures that are comining everything we covered so far: | |
// all primitive data types as well as objects and arrays | |
// ****************************************************** | |
// ****************** array of objects ****************** | |
// ****************************************************** | |
const students = [ | |
{ | |
firstName: 'Paola', | |
lastName: 'Sanchez', | |
bootcamp: 'Web Dev', | |
favorites: ['JavaScript', 'HTML'], | |
isCareerChanger: true | |
}, | |
{ | |
firstName: 'Mila', | |
lastName: 'Bjorn', | |
bootcamp: 'Data Analytics', | |
favorites: ['Python', 'SQL'], | |
isCareerChanger: true | |
}, | |
{ | |
firstName: 'Frank', | |
lastName: 'Bellagio', | |
bootcamp: 'UX', | |
favorites: ['Sketch', 'InVision'], | |
isCareerChanger: false | |
} | |
]; | |
console.log(students[1].firstName); // => Mila | |
console.log(students[0].isCareerChanger); // => true | |
console.log(students[2].firstName); // => Frank | |
console.log(students[2].favorites); // => [ 'Sketch', 'InVision' ] | |
console.log(students[0].favorites[0]); // => JavaScript | |
// loop over array of objects - example: | |
for(let i = 0; i < students.length; i++){ | |
console.log(students[i].firstName); | |
} | |
// => Paola, Mila, Frank | |
// ****************************************************** | |
// **************** array of arrays ********************* | |
// ****************************************************** | |
const developers = [ | |
['Ana', 'Will', 'Camilo'], | |
['Sandra', 'Miller', 'Chris'], | |
['Bjorn', 'Francisco', 'Robert'] | |
]; | |
console.log(developers[0][0]); // => Ana | |
console.log(developers[1][0]); // => Sandra | |
console.log(developers[2][2]); // => Robert | |
// loop over array of arrays - example: | |
for(let i = 0; i < developers.length; i++){ | |
console.log(developers[i][0]); | |
} | |
// => Ana, Sandra, Bjorn | |
// ****************************************************** | |
// ********** object with nested objects *************** | |
// ****************************************************** | |
const classRoom = { | |
teacher: { | |
firstName: 'Greg', | |
lastName: 'Dach', | |
age: 38, | |
address: { | |
street: "3085 Kelton Knolls", | |
city: "Aldaside", | |
state: "Maryland" | |
} | |
} | |
}; | |
console.log(classroom.teacher.firstName); // <== Greg | |
console.log(classroom.teacher.address.city); // <== Aldaside | |
// THE SAME RULES APPLY TO ADD/REMOVE ELEMENTS TO/FROM ARRAY (.push(), .pop(), etc.) | |
// students can practice this on their own |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment