forked from taiansu/astro_04_js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2.js
85 lines (68 loc) · 1.63 KB
/
day2.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
let fruits = ['apple', 'banana', 'cherry'];
// console.log(fruits.join(''))
/* indexOf */
// console.log(fruits.indexOf()) // => -1
// console.log(fruits.indexOf('apple')) // => 0
// console.log(fruits.indexOf('cherry')) // => 0
/* concat */
fruits = fruits.concat(['pear', 'watermelon'])
// console.log(fruits)
/* slice */
// console.log(fruits.slice(2, 3))
// console.log(fruits)
/* splice */
// console.log(fruits.splice(2, 3))
// console.log(fruits)
fruits = [ 'apple', 'banana', 'cherry', 'pear', 'watermelon']
// ^
for (let i = 0; i < fruits.length; i ++) {
// console.log(`I love ${fruits[i]}`)
}
for (let f of fruits) {
// console.log(`I got some ${f}`)
}
function printSomeItems(items) {
items = items.slice(1, 4);
for(let item of items) {
console.log(`I love ${item}`)
}
}
// printSomeItems(fruits)
/* variable scope */
// function foo() {
// let someVariable = 100;
// }
// console.log(someVariable);
/* Object */
function getCurrency(country) {
let currencies = {
us: 'USD',
tw: 'NTD',
jp: 'JPY'
}
return currencies[country]
}
console.log(getCurrency('jp'))
let studentA = {
name: 'John',
age: 18,
gender: 'M',
merried: false,
}
console.log(studentA.name)
// console.log(Object.keys(studentA))
// console.log(Object.values(studentA))
let entrs = Object.entries(studentA)
console.log(entrs)
let entrsWithoutMerried = []
for (let pair of entrs) {
if (pair[0] !== 'merried') {
entrsWithoutMerried.push(pair)
}
}
console.log(Object.fromEntries(entrsWithoutMerried))