Destructuring Objects Flashcards

1
Q

What is the destructuring of objects?

A

A javascript syntax that allow you to etract the data from an object in new top level variables without manually having to declare them.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the syntax for destructuring an object?

A
const person = {
    first: 'Wes',
    last: 'Bos
}
const { first, last } = person;
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you destructure nested data?

A

const { twitter, facebook } = wes.link.social;

const wes = {
    link: {
        social: {
            twitter: "url",
            facebook: "url"
        }
    }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In the process of destructuring, how would you rename the new variable?

A

the second param here is the new var name

const { twitter:tweet, facebook:fb } = wes.links.social

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If you are destructuring from an object but that object doesn’t have all the required variable what can you do?

A

Set default parameters in the destructuring syntax. These will be used if the variable isn’t present in the object being used.

const setting = { width: 300, color: 'black'  }
const width { width = 100, height = 100, fontSize = 25 }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What will the outcome be of this destructuring expression be and why?

const { w: width = 400, h: height = 500} = { w: 800 }

A
width = 800;
height = 500;

w is found in the object and so it’s value is used. The h is not found and so it’s default of 500 in the destructuring expression is used instead.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How would you destructure an array?

A

Just like an object but using square brackets:

const data = [name, id, website] = details;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

how would you destructure this?

const data = ‘basketball,sports,90210,23,web,bos,cool’;

A

const [itemName, category, sku, inventory] = data.split(‘,’);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How would you group together the rest of the extra variables in an array while destructuring?

Wes is Captain,
Harry is Assistant
and the rest are players?

const team = [‘wes’, ‘harry’, ‘sarah’, ‘keegan’, ‘Riker’];

A

Using the rest operator.

const team = [‘wes’, ‘harry’, ‘sarah’, ‘keegan’, ‘Riker’];

const [captain, assistant, …players] = team

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How would you swap two variables with destructuring?

let inRing = 'hulk hogan';
let onSide = 'The rock';

And why is this helpful?

A

[inRing, onSide] = [onSide, inRing];

This will swap the variable preventing the need for a temporary variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How would you destructure a return function and what would flexibility does it allow you?

A

convertCurrency function returns an object with multiple values.

const { USD, AUD } = convertCurrency(100);

You can choose just the property you need in any order because it is an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When destructuring an object into a function, how would you write it?

A

with curly brackets.

function tipCalc( { total = 100, tip = 0.15, tax = 0.13 } );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

When destructuring an object into a function without any parameters passed in what is wrong with this?

function tipCalc( { total = 100, tip = 0.15, tax = 0.13 } );

A

You get a desctructuring error because no object has been passed. The destructuring object it self then needs a default argument.

If this could happen you can add = {}

function tipCalc( { total = 100, tip = 0.15, tax = 0.13 } = {} );

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the benefits of destructuring parameters in a function?

A
by adding curly brackets, and passing an object the function automatically create our variables for us with out the need to create them ourselves with something like 
let name = options.name;

This allows our function parameters to order independant.

You can leave off things when calling the function and the default will kick in.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly