You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Basic Data Structures: Check if an Object has a Property
2
+
// 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:
3
+
4
+
// users.hasOwnProperty('Alan');
5
+
// 'Alan' in users;
6
+
// // both return true
7
+
8
+
// We've created an object, users, with some users in it and a function isEveryoneHere, which we pass the users object to as an argument. Finish writing this function so that it returns true only if the users object contains all four names, Alan, Jeff, Sarah, and Ryan, as keys, and false otherwise.
9
+
10
+
// The users object only contains the keys Alan, Jeff, Sarah, and Ryan
11
+
// The function isEveryoneHere returns true if Alan, Jeff, Sarah, and Ryan are properties on the users object
12
+
// The function isEveryoneHere returns false if Alan, Jeff, Sarah, and Ryan are not properties on the users object
// Basic Data Structures: Generate an Array of All Object Keys with Object.keys()
2
+
// We can also generate an array which contains all the keys stored in an object using the Object.keys() method and passing in an object as the argument. This will return an array with strings representing each property in the object. Again, there will be no specific order to the entries in the array.
3
+
4
+
5
+
// Finish writing the getArrayOfUsers function so that it returns an array containing all the properties in the object it receives as an argument.
6
+
7
+
// The users object only contains the keys Alan, Jeff, Sarah, and Ryan
8
+
// The getArrayOfUsers function returns an array which contains all the keys in the users object
// Basic Data Structures: Iterate Through the Keys of an Object with a for...in Statement
2
+
// Sometimes you may need to iterate through all the keys within an object. This requires a specific syntax in JavaScript called a for...in statement. For our users object, this could look like:
3
+
4
+
// for (let user in users) {
5
+
// console.log(user);
6
+
// };
7
+
8
+
// // logs:
9
+
// Alan
10
+
// Jeff
11
+
// Sarah
12
+
// Ryan
13
+
// In this statement, we defined a variable user, and as you can see, this variable was reset during each iteration to each of the object's keys as the statement looped through the object, resulting in each user's name being printed to the console.
14
+
15
+
// NOTE:
16
+
// Objects do not maintain an ordering to stored keys like arrays do; thus a keys position on an object, or the relative order in which it appears, is irrelevant when referencing or accessing that key.
17
+
18
+
19
+
// We've defined a function, countOnline; use a for...in statement within this function to loop through the users in the users object and return the number of users whose online property is set to true.
20
+
21
+
// The users object contains users Jeff and Ryan with online set to true and users Alan and Sarah with online set to false
22
+
// The function countOnline returns the number of users with the online property set to true
// Basic Data Structures: Modify an Array Stored in an Object
2
+
// Now you've seen all the basic operations for JavaScript objects. You can add, modify, and remove key-value pairs, check if keys exist, and iterate over all the keys in an object. As you continue learning JavaScript you will see even more versatile applications of objects. Additionally, the optional Advanced Data Structures lessons later in the curriculum also cover the ES6 Map and Set objects, both of which are similar to ordinary objects but provide some additional features. Now that you've learned the basics of arrays and objects, you're fully prepared to begin tackling more complex problems using JavaScript!
3
+
4
+
5
+
// Take a look at the object we've provided in the code editor. The user object contains three keys. The data key contains five keys, one of which contains an array of friends. From this, you can see how flexible objects are as data structures. We've started writing a function addFriend. Finish writing it so that it takes a user object and adds the name of the friend argument to the array stored in user.data.friends and returns that array.
6
+
7
+
// The user object has name, age, and data keys
8
+
// The addFriend function accepts a user object and a friend string as arguments and adds the friend to the array of friends in the user object
9
+
// addFriend(user, "Pete") should return ["Sam", "Kira", "Tomo", "Pete"]
0 commit comments