Advertisement
makispaiktis

Codecademy - 26th Course (Example in Methods of Objects)

Dec 22nd, 2019 (edited)
500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. 1.
  4. In main.js there is an object, robot. We’d like to grab the property names, otherwise known as keys, and save the keys in an array which is assigned to robotKeys. However, there’s something missing in the method call.
  5.  
  6. Find out what we have to include by reading MDN’s Object.keys() documentation.
  7.  
  8. If you look at the example at MDN, it looks like Object.keys() takes an argument of an object instance.
  9.  
  10. 2.
  11. Object.entries() will also return an array, but the array will contain more arrays that have both the key and value of the properties in an object.
  12.  
  13. Declare a const variable named robotEntries and assign to it the entries of robot by calling Object.entries().
  14.  
  15. To find how to use Object.entries(), read the documentation at MDN.
  16.  
  17. Object.entries() is similar to Object.keys() in how it is called and what arguments it will take. Remember to check with the documentation!
  18.  
  19. 3.
  20. Now what if we want another object that has the properties of robot but with a few additional properties. Object.assign() sounds like a great method to use, but like the previous examples we should check Object.assign() documentation at MDN.
  21.  
  22. Declare a const variable named newRobot. newRobot will be a new object that has all the properties of robot and the properties in the following object: {laserBlaster: true, voiceRecognition: true}. Make sure that you are not changing the robot object!
  23.  
  24. When you use Object.assign() it is important to know which argument is the target or source(s). Take a look at MDN’s syntax section and play around with the examples at the top to fully understand how to use Object.assign().
  25.  
  26. */
  27.  
  28. const robot = {
  29.     model: 'SAL-1000',
  30.   mobile: true,
  31.   sentient: false,
  32.   armor: 'Steel-plated',
  33.   energyLevel: 75
  34. };
  35.  
  36. // What is missing in the following method call?
  37. const robotKeys = Object.keys(robot);
  38. console.log(robotKeys);
  39.  
  40. // Declare robotEntries below this line:
  41. const robotEntries = Object.entries(robot)
  42. console.log(robotEntries);
  43.  
  44. // Declare newRobot below this line:
  45. const newRobot = Object.assign({laserBlaster: true, voiceRecognition: true}, robot);
  46. console.log(newRobot);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement