The next thing we are doing in JavaScript is algorithm scripting. Not sure what that is yet. The notes at the end of this has to do with objects.
I don’t have much to write about today. I’ve been doing pretty good on posting just about every day this week. I can’t recall if I posted on Monday. Wait, I think I did! My memory just isn’t that great. I should be writing everything down. Maybe put it in my planner that I wrote that day. Merlin is sleeping right now. I don’t want to bother him but I do have to leave the room and go to Karissa’s room in a bit. She needs to pay Tommy back for something and I need to pay for her classes this semester. But I’m waiting a bit before waking up Merlin. It’s like when you have a baby and you don’t want to interrupt their sleep cause it’s the only time that you have peace and quiet. I am bringing Merlin to Karissa’s room so she can watch him when he is awake. I can work on the computer easier without watching Merlin.
I haven’t eaten lunch yet. Again, Merlin is sleeping. Kel is with her mom down in the city helping Chris run errands. So the house is pretty quiet now. I’m starting to get hungry. I’m going to have to wake him up soon.
I’m not sure what the plans are this weekend. Labor day weekend. Tommy has only three days of work this next weekend since he will be off next Friday. So I think next Friday we are heading to the zoo. It’s a nice place to get your walking in. I think we are walking tonight. The only problem I have with walking at night is that I can’t see and the path by our house doesn’t have any lights. But I need to get my workout in.
Ok, I need some food. I gotta wake up Merlin.
JavaScript notes…
———————————-
In the first object challenge we mentioned the use of bracket notation as a way to access property values using the evaluation of a variable. For instance, imagine that our foods object is being used in a program for a supermarket cash register. We have some function that sets the selectedFood and we want to check our foods object for the presence of that food. This might look like:
let selectedFood = getCurrentFood(scannedItem); let inventory = foods[selectedFood];
This code will evaluate the value stored in the selectedFood variable and return the value of that key in the foods object, or undefined if it is not present. Bracket notation is very useful because sometimes object properties are not known before runtime or we need to access them in a more dynamic way.
Now you know what objects are and their basic features and advantages. In short, they are key-value stores which provide a flexible, intuitive way to structure data, and, they provide very fast lookup time. Throughout the rest of these challenges, we will describe several common operations you can perform on objects so you can become comfortable applying these useful data structures in your programs.
In earlier challenges, we have both added to and modified an object’s key-value pairs. Here we will see how we can remove a key-value pair from an object.
Let’s revisit our foods object example one last time. If we wanted to remove the apples key, we can remove it by using the delete keyword like this:
delete foods.apples;
Now we can add, modify, and remove keys from objects. But what if we just wanted to know if an object has a specific property? JavaScript provides us with two different ways to do this. One uses the hasOwnProperty() method and the other uses the in keyword. If we have an object users with a property of Alan, we could check for its presence in either of the following ways:
users.hasOwnProperty('Alan'); 'Alan' in users;
Both of these would return true.
Sometimes you need to iterate through all the keys within an object. You can use a for…in loop to do this. The for…in loop looks like:
const refrigerator = { 'milk': 1, 'eggs': 12, }; for (const food in refrigerator) { console.log(food, refrigerator[food]);
This code logs milk 1 and eggs 12, with each key-value pair on its own line.
We defined the variable food in the loop head and this variable was set to each of the object’s keys on each iteration, resulting in each food’s name being printed to the console.
NOTE: Objects do not maintain an ordering to stored keys like arrays do; thus a key’s position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
We can also generate an array which contains all the keys stored in an object with the Object.keys() method. This method takes an object as the argument and returns an array of strings representing each property in the object. Again, there will be no specific order to the entries in the array.