Exercise on Objects in ES6
Now, we'll create a "Basket" object using the concepts of Objects.
We'll cover the following...
Exercise 1:
Suppose an array of firstName
, email
, basketValue
triples are given. Create ONE JavaScript expression that puts a default value of '-'
and 0
to the firstName
or basketValue
fields respectively, whenever the firstName
or the basketValue
keys are missing. Remember to name your new object, newBaskets
or your code won’t run.
// the baskets object is randomly generatedconsole.log("baskets: ");console.log(baskets);let newBaskets = {}
Explanation
The hard part of the solution is that we need one JavaScript expression. When it comes to transforming the value of an array, we usually use the map method. The shortest way of writing a map function is through using the arrow syntax. Inside the map, we have to fill in the default values in item. If a key is already given in item, it takes precedence. We will use ...