Skip to content

Commit 9a8c0ed

Browse files
committed
更新新
1 parent 2fef2b2 commit 9a8c0ed

File tree

7 files changed

+309
-0
lines changed

7 files changed

+309
-0
lines changed

.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/Map_Set.html

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Map和set</title>
6+
</head>
7+
<body>
8+
<script>
9+
'use strict'
10+
// Map
11+
let f = new Map([['jack',100],['rose',90],['tom',80]]);
12+
// 通过key 找到对应的 value
13+
let name =f.get('jack');
14+
console.log(name);
15+
16+
// 添加键值对
17+
f.set('lucy',50);
18+
console.log(f);
19+
20+
// 删除元素
21+
f.delete('jack');
22+
console.log(f);
23+
24+
// Set 可以去重
25+
let s = new Set([3,1,1,1,1]);
26+
// [3,1]
27+
28+
// 添加元素
29+
s.add(2);
30+
console.log(s);
31+
32+
// 删除元素
33+
s.delete(1);
34+
console.log(s);
35+
36+
// 查找元素
37+
console.log(s.has(3)); // 返回ture
38+
39+
40+
41+
42+
43+
44+
</script>
45+
</body>
46+
</html>

javascript/forEach详解

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
与for循环一样,forEach也属于完整遍历数组的方法,并会对数组每项元素执行提供的回调函数,一个完整的forEach应该是这样,我们一一解释回调函数的参数,与后方的this有何作用。
2+
3+
arr.forEach(function(self,index,arr){},this);
4+
5+
self:数组当前遍历的元素,默认从左往右依次获取数组元素。
6+
7+
index:数组当前元素的索引,第一个元素索引为0,依次类推。
8+
9+
arr:当前遍历的数组。
10+
11+
this:回调函数中this指向。
12+
13+
我们来看个简单的forEach例子,加强对于这四个参数的印象:
14+
15+
16+
// code
17+
18+
let arr1 = [1, 2, 3, 4];
19+
let obj = {
20+
a: 1
21+
};
22+
arr1.forEach(function (self, index, arr) {
23+
console.log(`当前元素为${self}索引为${index},属于数组${arr}`);
24+
//做个简单计算
25+
console.log(self + this.a);
26+
}, obj)
27+
28+
1.forEach不支持break
29+
30+
大家都知道,在使用for循环时可以使用break跳出循环,比如我希望找到数组中符合条件的第一个元素就跳出循环,这对于优化数组遍历是非常棒的。很遗憾,forEach并不支持break操作,使用break会导致报错。
31+
32+
// code
33+
34+
let arr = [1, 2, 3, 4],
35+
i = 0,
36+
length = arr.length;
37+
for (; i < length; i++) {
38+
console.log(arr[i]); //1,2
39+
if (arr[i] === 2) {
40+
break;
41+
};
42+
};
43+
44+
arr.forEach((self,index) => {
45+
console.log(self);
46+
if (self === 2) {
47+
break; //报错
48+
};
49+
});
50+
51+
52+
53+
那forEach能不能跳出循环呢?可以,不过不是使用break,而是结合try catch操作,有兴趣可是看看博主这篇文章 js forEach跳出循环
54+
55+
2.forEach中使用return无效
56+
57+
首先需要确定的,直接再for循环中使用return会报错(函数中使用for可以return),forEach中使用return不会报错,但rerutn并不会生效,我们来看个例子:
58+
59+
60+
// code
61+
62+
let arr = [1, 2, 3, 4];
63+
64+
function find(array, num) {
65+
array.forEach((self, index) => {
66+
if (self === num) {
67+
return index;
68+
};
69+
});
70+
};
71+
let index = find(arr, 2);// undefined
72+
73+
74+
75+
上述代码想要找到数字2在数组中的索引,但return并不会起到终止代码运行并返回值的作用。
76+
当然如果我们真的要用return返回某个值,那就只能将return操作放在函数中,而不是forEach循环中,像这样:
77+
78+
// code
79+
80+
function find(array, num) {
81+
let _index;
82+
array.forEach((self, index) => {
83+
if (self === num) {
84+
_index = index;
85+
};
86+
});
87+
return _index;
88+
};
89+
90+
91+
92+
93+
3.forEach删除自身元素index不会被重置
94+
还记得文章开头的问题吗,那段代码其实只会执行一次,数组也不会被删除干净,这是因为forEach在遍历跑完回调函数后,会隐性让index自增,像这样:
95+
96+
// code
97+
98+
arr.forEach((item, index) => {
99+
arr.splice(index, 1);
100+
console.log(1);
101+
//这里隐性让index自增加1
102+
index++;
103+
});
104+
105+
106+
107+
108+
当第一次遍历结束,此时数组为[2]而index变成了1,此时数组最大索引只是0,不满足条件,所以跳出了循环。
109+
灵机一动,有没有什么办法让此时的forEach不跳出循环呢,当然有,使用ES6的拓展运算符。
110+
// code
111+
112+
[...arr].forEach((item, index) => {
113+
arr.splice(index, 1);
114+
console.log(1);
115+
});
116+
117+
通过拓展运算符重置数组arr,达到不跳出循环的目的,你会发现内部确实执行了两次,很遗憾的是index依旧没被重置,所以数组arr还是无法在遍历的同时删空自己。
118+
因为在实际开发中,遍历数组同时删除某项的操作十分常见,所以对于习惯了forEach的同学,这一点一定要注意。
119+
120+
121+
122+
123+
124+
125+

javascript/iterator.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>iterator</title>
6+
</head>
7+
<body>
8+
<script>
9+
'use strict'
10+
// 遍历Map 和 Set 用 for.. of
11+
12+
let f = new Map([['jack',100],['rose',90],['tom',80]]);
13+
for (let x of f){
14+
console.log(x);
15+
}
16+
17+
let s = new Set([3,1,1,1,1]);
18+
for (let b of s){
19+
console.log(b);
20+
}
21+
22+
23+
</script>
24+
</body>
25+
</html>

javascript/基本语法_03.html

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>数组</title>
6+
</head>
7+
<body>
8+
<script type="text/javascript">
9+
'use strict'
10+
// 数组
11+
let arr = [1,2,3,4];
12+
console.log(arr);
13+
console.log(arr.length);// 计算过长度 输出:4
14+
15+
arr.indexOf(2);// 通过元素找到下标
16+
17+
arr.slice(1,3);
18+
// 截取数组的一部分
19+
// 输出:Array [ 2, 3 ] 左闭右开[ )
20+
21+
22+
arr.push('a','b');
23+
// Array(6) [ 0, 2, 3, 4, "a", "b" ]
24+
arr.pop();
25+
// Array(6) [ 0, 2, 3, 4, "a" ]
26+
27+
arr.unshift(6);
28+
//[ 6,0, 2, 3, 4, "a" ]
29+
arr.shift();
30+
31+
32+
console.log(arr.join('-'));
33+
// 1-2-3-4-a
34+
35+
</script>
36+
</body>
37+
</html>

javascript/基本语法_04.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>对象</title>
6+
</head>
7+
<body>
8+
<script type="text/javascript">
9+
'use strict'
10+
11+
// 格式
12+
let person = {
13+
name:"jack",
14+
age:10,
15+
score:90
16+
17+
}
18+
19+
// 对象赋值
20+
person.age=20;
21+
console.log(person.age); // age: 20
22+
23+
delete person.age;
24+
// age 属性被删除
25+
26+
person.weight= 100;
27+
// 新 weight 键值对添加
28+
29+
30+
</script>
31+
</body>
32+
</html>

javascript/基本语法_05.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>流程控制</title>
6+
</head>
7+
<body>
8+
<script>
9+
'use strict'
10+
let age =3;
11+
if(age>3){
12+
console.log('happy');
13+
}else console.log('sad');
14+
// sad
15+
16+
// while 不做例子
17+
18+
// for of 遍历元素 for in 遍历key
19+
let num = [1,2,3,4,5,6,7,9];
20+
for (let number of num) {
21+
console.log(number);
22+
}
23+
24+
25+
let arr1 = [1, 2, 3, 4];
26+
let obj = {
27+
a: 1
28+
};
29+
arr1.forEach(function (self, index, arr) {
30+
console.log(`当前元素为${self}索引为${index},属于数组${arr}`);
31+
//做个简单计算
32+
console.log(self + this.a);
33+
}, obj)
34+
35+
36+
</script>
37+
</body>
38+
</html>

0 commit comments

Comments
 (0)