Objects Lab
Problem Statement
We covered the concepts of Object
s in JavaScript. Now it's time to put the
concepts into practice.
Objectives
- Create
Object
s - Perform operations on
Object
s
Instructions
Be sure to run the tests to get a feel for the types of problems this lab is
asking you to solve. In particular, you'll need to define a driver
Object
and then apply certain updates (destructively and non-destructively) in various
functions.
You'll be writing four functions:
updateDriverWithKeyAndValue()
- this function should take in adriver
Object
, akey
and avalue
. The function should not mutate thedriver
parameter and return a newdriver
that has an updatedvalue
for thekey
passed in.destructivelyUpdateDriverWithKeyAndValue()
- this function should work the same asupdateDriverWithKeyAndValue()
but it should mutate thedriver
parameter passed in.deleteFromDriverByKey()
- this function should take in adriver
Object
and akey
. It should delete thekey
/value
pair for thekey
that was passed in from thedriver
Object
. This should all not actually mutate thedriver
passed in.destructivelyDeleteFromDriverByKey()
- this function should work the same asdeleteFromDriverByKey()
but it should mutate thedriver
passed in.
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!
Conclusion
In this lab, we worked with creating Object
s and performing operations on
them.
Resources
View Objects Lab on Learn.co and start learning to code for free.