Skip to content

Commit 7ecd1ae

Browse files
committed
destructuring lessons done
1 parent 2cf9e98 commit 7ecd1ae

File tree

1 file changed

+64
-43
lines changed

1 file changed

+64
-43
lines changed

app/index.js

Lines changed: 64 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,82 @@
1-
//Rest and Spread
1+
//destructuring
22

3-
function addNums(numbers) {
4-
return numbers.reduce((total, number) => {
5-
return total + number;
6-
},0)
3+
var expense = {
4+
type: 'Business',
5+
amount: '$45 USB'
76
}
7+
//
8+
// var type = expense.type;
9+
// var amount = expense.amount;
810

9-
const a = [1, 2, 3, 4, 5, 6, 7];
11+
//es6
12+
//when curly braces used on left side, it is saying you want to declare a new variable called whatever is inside the
13+
//brace and it should reference the right hand expression.
1014

11-
console.log(addNums(a));
15+
//here we are saying we want to declare a variable called type and assign it to expense.type.
16+
// const { type } = expense;
17+
// const { amount } = expense;
1218

13-
//this says we don't know how many arguments are coming in, we want to capture them all and put them into
14-
//an array the ... is a rest operator that allows us to do this.
19+
//can simplify even more, we can pull off more than one variable within an object with a comma separated list
20+
const { type, amount } = expense;
1521

16-
function addNums1(...numbers) {
17-
return numbers.reduce((total, number) => {
18-
return total + number;
19-
},0)
22+
23+
var savedFile = {
24+
extension: '.jpg',
25+
name: 'repost',
26+
size: 14040
27+
};
28+
29+
function fileSummary(file) {
30+
return `The file ${file.name}${file.extension} is of size ${file.size}`
2031
}
2132

33+
console.log(fileSummary(savedFile));
2234

23-
console.log(addNums1(1,2,3,4,5,6,7));
35+
//destructure
2436

37+
function fileSummary1({ name, extension, size}) {
38+
return `The file ${name}${extension} is of size ${size}`
39+
}
2540

26-
const defaultColors = ['red', 'green'];
27-
const userFavColors = ['orange', 'yellow'];
41+
console.log(fileSummary1(savedFile));
2842

29-
console.log(defaultColors.concat(userFavColors));
43+
//arrays
3044

31-
//this is the spread operator, a new array was created with the [] brackets, we then put inside of it, a reference
32-
//to existing arrays defaultColors and userFavColors we then added the spread operator which is the ... which means
33-
//we want to take all of what is in the referenced array and pull them out into the new array. The same process is
34-
//repeated with all spread operators followed by an array. It removes whatever was in the referenced array and puts it
35-
//into the new array.
36-
console.log([...defaultColors, ...userFavColors]);
37-
//can also add in new elements at the same time...
38-
console.log(['blue',...defaultColors, 'green', ...userFavColors]);
45+
//when desstructing arrays, you get them in the order they are in the original array
46+
const companies = ['google', 'facebook', 'Uber'];
47+
//
48+
// const [ name, name1, name2 ] = companies;
49+
// console.log(name, name1, name2);
3950

4051

41-
//here we are using both rest and spread. First a list of items of any size is sent in. We check to see
42-
//if there is milk on the list, if not, we add it to the list of items and return it. Otherwise we just return the list.
43-
function validateShoppingList(...items) {
44-
if (items.indexOf('milk') < 0) {
45-
return ['milk', ...items];
46-
}
47-
return items;
48-
}
52+
const companies2 = [
53+
{name: 'google', location: 'Mountain View'},
54+
{name: 'facebook', location: 'Menlo Park'},
55+
{name: 'Uber', location: 'San Francisco'}
56+
];
57+
58+
//goes outside in. so first we grab the first index of the array which is the full obj. then, with the {} we
59+
//are destructuring further and getting the name and location.
60+
const [{name, location}] = companies2;
61+
console.log(name, location);
62+
63+
64+
const Google = {
65+
locations: ['Mountain View', 'New York', 'London']
66+
};
67+
//more chanllenging...we are first destructing obj. and getting locations. However, this is an array so we need
68+
//to walk into the array and pull off the first item of that array
69+
// const {locations : [ location ]} = Google;
70+
// console.log(location);
4971

50-
console.log(validateShoppingList('oranges', 'bread', 'eggs'));
72+
const points = [
73+
[4,5],
74+
[10, 1],
75+
[0, 40]
76+
];
5177

78+
const list = points.map(([x, y]) => {
79+
return { x: x, y: y};
80+
});
5281

53-
const MathLibrary = {
54-
calcProduct (...rest) {
55-
console.log('please use multNums instead');
56-
return this.multNums(...rest)
57-
},
58-
multNums(a,b) {
59-
return a*b;
60-
}
61-
};
82+
console.log(list);

0 commit comments

Comments
 (0)