We covered the concepts of Object
s in JavaScript. Now it's time to put the
concepts into practice.
- Create
Object
s - Perform operations on
Object
s
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: adriver
Object
, akey
and avalue
. This function should not mutate thedriver
and should 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. Be sure and consider whether dot-notation or 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 Object
s and performing operations on
them.