We covered the concepts of Objects in JavaScript. Now it's time to put the
concepts into practice.
- Create
Objects - Perform operations on
Objects
Follow the steps below, running learn as you go to get additional information
from the tests. To start, define a driver variable and assign it to an
Object. Various updates will be applied to this variable (destructively and
non-destructively) in this lab.
You'll be writing four functions:
updateDriverWithKeyAndValue()- this function should take in three arguments: adriverObject, akeyand avalue. This function should not mutate thedriverand should return a newdriverthat has an updatedvaluefor thekeypassed in.destructivelyUpdateDriverWithKeyAndValue()- this function should work the same asupdateDriverWithKeyAndValue()but it should mutate thedriverparameter passed in.deleteFromDriverByKey()- this function should take in adriverObjectand akey. It should delete thekey/valuepair for thekeythat was passed in from thedriverObject. This should all not actually mutate thedriverpassed in.destructivelyDeleteFromDriverByKey()- this function should work the same asdeleteFromDriverByKey()but it should mutate thedriverpassed in. Be sure to consider whether and how using dot notation vs. bracket notation might affect your solution.
HINT: You might find deleteFromDriverByKey() to be a bit hard to write
non-destructively. Think about how we learned to use Object.assign(). What
happens if we do this:
const obj = { foo: "bar" };
const newObj = Object.assign({}, obj);
newObj;
// => { foo: "bar" }
delete newObj['foo'];
// => true
newObj;
// => {}
obj;
// => { foo: "bar" }Something to keep in mind!
In this lab, we worked with creating Objects and performing operations on
them.