Destructuring Flashcards

1
Q

Does the variable name have to equal the property name?

A

Yes

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

How would you destructure off a property of an object?

A

Something like:

var saveFiled = {
 extension: 'jpg',
 name: 'repost',
 size: 14040
}
function fileSummary({name, extension, size}, {color}){
 return `The ${color} file ${name}.${extension} is of size ${size}`;
}

fileSummary(saveFiled, {color: ‘red’});

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

How would you destructure an array?

A

Destructuring an array is all about pulling off elements

Remember, when destructuring a property you use {} for array you use []

const companies = [
‘google’,
‘facebook’,
‘uber’

];

const [name, …rest] = companies;

name
rest

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

What through an object with array as value

A
//const companies = [
 {name: 'Google', location: 'Mountain View'},
 {name: 'Facebook', location: 'Menlo Park'},
 {name: 'Uber', location: 'San Francisco'}
 ];

// const [{location}] = companies;

// location

const Google = {
 locations: ['LA', 'SF', 'NYC'] 
}

const {locations:[location]} = Google;
location;

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

When would you want to use desctructuring? Is there anything more than cleaning our code?

A

When you have a bunch of arguments that are coming from a, say, user object and you destructure the properties from the object, it doesn’t matter what the order is!

const points = [
[4,5],
[3,10]
];

points.map(([x,y]) =>{
return {x,y};
});

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