Skip to content

1.html5中的details标签

html
<details>
  <summary><b>Tags</b></summary>
  array | hash-table
</details>
  • <details>:这是包含可折叠部分的容器元素。当部分被折叠时,它会显示一个摘要,当展开时,将显示其中的内容。
  • <summary>:这个元素用于定义可折叠部分的标题或头部。在这里,它包含了加粗的文本 "Tags"。

2.forEach循环的痛点以及与for循环的比较

介绍:

for循环是js提出时就有的循环方法。forEach是ES5提出的,挂载在可迭代对象原型上的方法,例如Array Set Map。

forEach是一个迭代器,负责遍历可迭代对象。

forEach 的参数:arr.forEach((self,index,arr) =>{},this)

self: 数组当前遍历的元素,默认从左往右依次获取数组元素。 index: 数组当前元素的索引,第一个元素索引为0,依次类推。 arr: 当前遍历的数组。 this: 回调函数中this指向。

1.forEach 删除自身元素,index不可被重置

在 forEach 中我们无法控制 index 的值,它只会无脑的自增直至大于数组的 length 跳出循环 (index从0开始连续增大,不管当前数组是否改变,一旦遍历完所有就结束循环)。所以也无法删除自身进行index重置,先看一个简单例子:

js
let arr = [1,2,3,4]
arr.forEach((item, index) => {
    console.log(item); // 1 2 3 4,改变了index但是输出没有任何变化
    index++;
});

index不会随着函数体内部对它的增减而发生变化。在实际开发中,遍历数组同时删除某项的操作十分常见,在使用forEach删除时要注意。

而 for 循环可以控制循环起点

如上文提到的 forEach 的循环起点只能为0不能进行人为干预,而for循环不同:

js
let arr = [1, 2, 3, 4],
    i = 1,
    length = arr.length;

for (; i < length; i++) {
    console.log(arr[i]) // 2 3 4
};

实例:

forEach在循环内删除元素,是会跳一位index的

比如下面这个例子:我们删除数组中所有等于1的元素

js
let arr = [1, 2, 1, 4, 5, 1, 6, 7];
arr.forEach((e, index) => {
  console.log(e + "_" + index); //2、4、6都被略过了,因为1删除之后,后面的顺位补过来,那么下一个index输出后一位了,相当于跳了两位
  if (e === 1) {
    arr.splice(index, 1);
  }
});
console.log(arr);
image-20230726014908720

上面这里我们发现,删除是成功的,但是2、4、6都被略过了,并且如果1是连着的也不会删干净,比如下面这样子:

js
let arr = [1, 1, 1, 4, 5, 1, 1, 7];
arr.forEach((e, index) => {
  console.log(e + "_" + index);
  if (e === 1) {
    arr.splice(index, 1);
  }
});
console.log(arr); //这里我们发现,根本删不干净,因为删除1之后,紧接着后面的元素是会被跳过的,不会遍历到,自然也没有发现它等于1
image-20230726014847180

使用for循环来处理:

js
let arr = [1, 2, 1],
    i = 0,
    length = arr.length;
for (; i < length; i++) {
    // 删除数组中所有的1
    if (arr[i] === 1) {
        arr.splice(i, 1);
        //重置i,否则i会跳一位
        i--;
    };
};
console.log(arr); // [2]
//等价于:filter方法
var arr1 = arr.filter(index => index !== 1);
console.log(arr1) // [2]

2.forEach 的中断很难

在js中有break return continue 对函数进行中断或跳出循环的操作,我们在 for循环中会用到一些中断行为,对于优化数组遍历查找是很好的,但由于forEach属于迭代器,只能按序依次遍历完成,所以不支持上述的中断行为。

1.如果我一定要在 forEach 中跳出循环呢?其实是有办法的,借助try/catch:

注意:会终止整个循环,相当于break,比较好用

js
try {
	var arr = [1, 2, 3, 4];
	arr.forEach(function (item, index) {
		//跳出条件
		if (item === 3) {
			throw new Error(“LoopTerminates”); //直接跳到下面,然后结束循环
		}
		//do something
		console.log(item);
	});
} catch (e) {
	if (e.message !== “LoopTerminates”) throw e;
};
image-20230726015920553

2.forEach中 若遇到 return 并不会报错,但是不会生效:实际上会终止本次循环,相当于continue

js
let arr = [1, 2, 3, 4];

function find(array, num) {
    array.forEach((self, index) => {
        if (self === num) {
            return index;
        };
    });
};
let index = find(arr, 2);// undefined