DeDestructuring Flashcards
What is array destructuring?
Destructuring the array in JavaScript simply meansextracting multiple values from data stored in objects and arrays
What is the syntax for array destructuring?
let array = [1,2,3];
const [a, b] = array;
how can you skip over values in a destructuring assignment?
you can use multiple commas to skip over specific elements.
let array = [1,2,3];
const [a,, b] = array;
How can you collect the rest of the values into a new array?
you can use the rest operator to collect the rest of the values into a new array.
let array = [1,2,3,4,5];
const [a, b, …rest] = array;
How can you use the spread operator to create a new array from 2 or more existing arrays?
let array1 = [1,2,3,4,5];
let array2= [a,b,c,d];
const newArray = […array1, …array2];
How can we deal with uncertainty of if an array has a value in a specific position?
We can set default parameters on destructuring assignments as well. If the array in question does not contain a value in that position, the default value will be used.
let double=[12,24,48];
const [two, four, eight, sixteen=NaN] = double;
Object destructuring syntax
let Conor = {
age: 24,
name: ‘Conor’,
hot: true;
}
const {age, name}= Conor;
//age=== 24
//name===’Conor’
how do we set new titles in an object destructuring?
let Conor = {
age: 24,
name: ‘Conor’,
hot: true;
}
const {age:yearsOnEarth, name:title}= Conor;
//yearsOnEarth=== 24
//title===’Conor’
how would you get the city from this object:
let Conor = {
age: 24,
name: ‘Conor’,
hot: true,
location: {
country: ‘Thailand’,
city: ‘Chiang Mai’,
}
}
const {name, location: {city}} = Conor;
how does destructuring work when you combine 2 objects?
when using destructuring to combine two objects, the first object is destructured and its properties are assigned to the new object and then the second object is destructured and assigned. If the first and second object contain any of the same keys, the value from the first object in the new object will be overwritten by the value in the second.
What does the spread operator do?
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected, or an object expression to be expanded in places where zero or more key-value pairs (for object literals) are expected.
Where can you use spread syntax?
- In function calls
- In array literals
- In object literals
result of: …“BANANAS”
B A N A N A S
combine these objects using the spread operator:
const mainColors = {brightRed: “#e55039”, waterfallBlue: “#38ada9”};
const accentColors = {lightGrey: “#778ca3”, swanWhite: “#f7f1e3”};
const fullPalette = {…mainColors, …accentColors};
If we want to use an object as an argument but only need a few of its properties, how can we use object destructuring to do that?
let Conor = {
age: 24,
name: ‘Conor’,
hot: true,
location: {
country: ‘Thailand’,
city: ‘Chiang Mai’,
}
}
function greetUser({name, location: {city}}) {
console.log(Greetings ${name}! How is the weather in ${city}?
);
}