-
Given this array:
let arr1 = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight'];
Using a single destructuring assignment, assign to three variables,
a
,b
andc
so thata
containsone
,b
containstwo
, andc
contains all of the remaining parameters as an array. -
What does is the value of
p3
here?let phonetic = ['sierra', 'charlie']; let [p1, p2, p3] = phonetic;
How do you think you could default
p3
to a suitable value? Try it. -
Given this array:
let arr2 = ['one', 'two', ['three', ['four', ['five', 'six', 'seven']], 'eight']];
Create a destructuring pattern to give this result:
console.log(m, n, o); // two six eight
-
Assume you have this object:
let stats = { packetForwarded: 5345, redirectSent: 427, forwardCacheHit: 4821, forwardCacheMiss: 524, };
-
Use destructuring to assign variables such that:
console.log(hits); // 4821 console.log(misses); // 524
-
What happens when this line is executed? Why, and how would you fix it?
{ forwardCacheHit: myhits, forwardCacheMiss: mymisses } = stats;
-
-
Can you destructure a string? Try it.
-
These contrived functions connect to a server and extract data from a 'config' object:
function connectTo(host, port) { console.log('Connecting to ' + host + ' on port ' + port); } function initialize(configObj) { var host = configObj.host; var port = configObj.port; connectTo(host, port); } initialize({host: 'localhost', port: 8080});
- Make suitable conversions to these functions by using ES6 syntax.
- If
host
orport
are not provided, default them tolocalhost
and8080
respectively.