|
| 1 | +## 06-Objects-The-Core-of-Javascript |
| 2 | + |
| 3 | +## 1. Intro to Objects |
| 4 | + |
| 5 | +### How would you store this? |
| 6 | + |
| 7 | +Health App, User dashboard |
| 8 | +- 308.272 total steps |
| 9 | +- 1.826 total floors |
| 10 | +- 211.70 total miles |
| 11 | +- 5.755 avg. daily calorie burn |
| 12 | +- 2312 total active minutes |
| 13 | +- 7 of 5 days exercising this week |
| 14 | +- 2 hrs 13 mins of restful sleep |
| 15 | + |
| 16 | +If we used an array, we definitely could, It just would be confusing and we'd have to come up with some arbitrary rules. |
| 17 | + |
| 18 | +### OBJECTS |
| 19 | +- Objects are collections of properties |
| 20 | +- Properties are a key-value pair |
| 21 | +- Rather than accessing data using an index, we use custom keys. |
| 22 | + |
| 23 | +https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object |
| 24 | + |
| 25 | +In objects we don't use a number to access our data out like we do with an array where we say, give me the index of one item instead, Example: give me city, please or give me zip or age and you get the corresponding value. |
| 26 | + |
| 27 | +You have a key and it's kind of a one way relationship, you put the key in and you figure out the corresponding value. |
| 28 | + |
| 29 | +### PROPERTY = KEY + VALUE |
| 30 | + |
| 31 | +``` |
| 32 | +username: '@ElfGodd', |
| 33 | +age: 36, |
| 34 | +city: 'Barranquilla', |
| 35 | +zip: 101001, |
| 36 | +text: 'great post!', |
| 37 | +upvotes: '7', |
| 38 | +isAdmin: true |
| 39 | +``` |
| 40 | + |
| 41 | +### AN OBJECT! |
| 42 | +``` |
| 43 | +const fitAppData = { |
| 44 | + totalSteps: 308727, |
| 45 | + totalMiles: 211.7, |
| 46 | + avgCalorieBurn: 5755, |
| 47 | + workoutsThisWeek: '5 of 7', |
| 48 | + avgGoodSleep,: '2:13' |
| 49 | +}; |
| 50 | +``` |
| 51 | +ALL TYPES WELCOME! |
| 52 | +``` |
| 53 | +let comment = { |
| 54 | + username: 'sillyGoose420', |
| 55 | + downVotes: 19, |
| 56 | + upVotes: 214, |
| 57 | + netScore: 195, |
| 58 | + commentText: 'Tastes like chicken lol', |
| 59 | + tags: ['#hilarious', '#funny', '#silly'], |
| 60 | + isGilded: false |
| 61 | +}; |
| 62 | +``` |
| 63 | + |
| 64 | +## 2. Creating Object Literals |
| 65 | + |
| 66 | +### VALID KEYS |
| 67 | +All keys are converted to strings, Except for Symbols |
| 68 | +`key: value === string: data` |
| 69 | + |
| 70 | +## 3. Accessing Object Properties |
| 71 | + |
| 72 | +### ACCESSING DATA |
| 73 | + |
| 74 | +``` |
| 75 | +// This is converted |
| 76 | +const numbers = { |
| 77 | + 100: 'one hundred', |
| 78 | + 16: 'sixteen' |
| 79 | +} |
| 80 | +// to this |
| 81 | +const numbers = { |
| 82 | + '100': 'one hundred', |
| 83 | + '16': 'sixteen' |
| 84 | +} |
| 85 | +
|
| 86 | +``` |
| 87 | + |
| 88 | +Why would you use square brackets aside from if you tried to store numbers as a key, Well sometimes you have property names that may not be valid JavaScript identifiers. Now, an identifier in JS is like the name of a variable. |
| 89 | + |
| 90 | +``` |
| 91 | +// invalid name variable |
| 92 | +// invalid indentifier or unexpected token |
| 93 | +// i cannot have a variable name, start with a number |
| 94 | +let my cat = 'Elf Cat' |
| 95 | +let 76games = 'NBA team' |
| 96 | +
|
| 97 | +// But that doesn't mean we can't do that as a key in an object |
| 98 | +object = { |
| 99 | + '76 trombones': 'great song!' |
| 100 | +} |
| 101 | +console.log(numbers2['76 trombones']); // 'great song!' |
| 102 | +``` |
| 103 | + |
| 104 | +Another reason sometimes you want to use a dynamic value like a variable to access data from an object. |
| 105 | + |
| 106 | +``` |
| 107 | +const palette = { |
| 108 | + red: '#eb4d4b', |
| 109 | + yellow: '#f9ca24', |
| 110 | + blue: '#30336b' |
| 111 | +} |
| 112 | +
|
| 113 | +palette.red // '#eb4d4b' |
| 114 | +palette['blue'] // '#30336b' |
| 115 | +
|
| 116 | +let color = 'yellow'; |
| 117 | +palette.color // undefined property doesn't exist 'color' |
| 118 | +palette[color] // '#f9ca24' |
| 119 | +``` |
| 120 | + |
| 121 | +My general rule of thumb is to try and use the dot notation whenever |
| 122 | +possible. It is shorter and nicer to look, but when it's not possible, whether it's because you have a key name that is not valid |
| 123 | +as a identifier like **let 76games = 'NBA team'** |
| 124 | + |
| 125 | +## 4. Adding and Updating Properties |
| 126 | + |
| 127 | +UPDATING & ADDING PROPERTIES |
| 128 | +// Updating properties: fitBitData.workoutsThisWeek = '6 of 7'; fitBitData.totalMiles += 7.5; |
| 129 | + |
| 130 | +// Adding a new property fitBitData.heartStillBeating = true; |
| 131 | + |
| 132 | +ARRAYS + OBJECTS |
| 133 | +const shoppingCart = [ { product: 'Jenga Classic', price: 6.88, quantity: 1 }, { product: 'Echo Dot', price: 29.99, quantity: 3 }, { product: 'Fire Stick', price: 39.99, quantity: 2 } ] |
| 134 | + |
| 135 | +const student = { firstName: 'David', lastName: 'Jones', strengths: ['Music', 'Art'], exams: { midterm: 92, final: 88 } } |
| 136 | + |
| 137 | +Nested Arrays & Objects |
| 138 | +const game = { player1: { username: 'Blue', playingAs: 'X' }, player2: { username: 'Muffins', playingAs: '0' }, board : [ ['0', null, 'X'], ['X', '0', 'X'], [null, '0', 'X'] ] }; |
| 139 | + |
| 140 | +Objects and Reference Types |
| 141 | +Objects and Arrays are Reference types |
| 142 | + |
| 143 | +Array Object Equality |
| 144 | +Arrays and Objects behave the same way when we checking for Equality We are comparing Reference Types Addresses |
0 commit comments