Destructuring Flashcards
Does the variable name have to equal the property name?
Yes
How would you destructure off a property of an object?
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 would you destructure an array?
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
What through an object with array as value
//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;
When would you want to use desctructuring? Is there anything more than cleaning our code?
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};
});