DeDestructuring Flashcards

1
Q

What is array destructuring?

A

Destructuring the array in JavaScript simply meansextracting multiple values from data stored in objects and arrays

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

What is the syntax for array destructuring?

A

let array = [1,2,3];
const [a, b] = array;

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

how can you skip over values in a destructuring assignment?

A

you can use multiple commas to skip over specific elements.
let array = [1,2,3];
const [a,, b] = array;

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

How can you collect the rest of the values into a new array?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can you use the spread operator to create a new array from 2 or more existing arrays?

A

let array1 = [1,2,3,4,5];
let array2= [a,b,c,d];
const newArray = […array1, …array2];

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

How can we deal with uncertainty of if an array has a value in a specific position?

A

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;

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

Object destructuring syntax

A

let Conor = {
age: 24,
name: ‘Conor’,
hot: true;
}

const {age, name}= Conor;
//age=== 24
//name===’Conor’

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

how do we set new titles in an object destructuring?

A

let Conor = {
age: 24,
name: ‘Conor’,
hot: true;
}

const {age:yearsOnEarth, name:title}= Conor;
//yearsOnEarth=== 24
//title===’Conor’

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

how would you get the city from this object:
let Conor = {
age: 24,
name: ‘Conor’,
hot: true,
location: {
country: ‘Thailand’,
city: ‘Chiang Mai’,
}
}

A

const {name, location: {city}} = Conor;

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

how does destructuring work when you combine 2 objects?

A

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.

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

What does the spread operator do?

A

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.

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

Where can you use spread syntax?

A
  1. In function calls
  2. In array literals
  3. In object literals
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

result of: …“BANANAS”

A

B A N A N A S

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

combine these objects using the spread operator:
const mainColors = {brightRed: “#e55039”, waterfallBlue: “#38ada9”};
const accentColors = {lightGrey: “#778ca3”, swanWhite: “#f7f1e3”};

A

const fullPalette = {…mainColors, …accentColors};

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

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?

A

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}?);
}

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

Math.min() expects accepts any number of arguments. How can we use the spread operator to make it accept an array instead?

A

const temperatures = [76,72,68,79,54,65];
Math.min(…temperatures)
//the same as Math.min(76,72,68,79,54,65)

17
Q

How do you use spread to create a new array from existing arrays?

A

const parents = [“Lorie”, “Steve”];
const kids = [‘Jake’, “Annie”, “Jess”];

const fullFamily = […parents, …kids];
//[“Lorie”, “Steve”, “Jake”, “Annie”, “Jess”]

18
Q

How can you use the spread operator to copy an array?

A

const originals = [“Mona Lisa”, “American Gothic”, “The School of Athens”];
const copies = […originals];

19
Q

In some cases such as when writing react, it is important not to mutate arrays and objects directly. How can we use spread to get around this?

A

const todos = [
{user: “Brick”, completed: false, task: “Upload Video”},
{user: “Lilith”, completed: true, task: “Rob Bank”}
]

function addTodo(newTodo){
return […todos, {…newTodo, completed: false}]
}

const newTodos = addTodo({user: “Mordecai”, task: “Feed Bloodwing”});

20
Q
A