一:Set集合对象(去重)
// 使用: new Set()
const s = new Set();
// 1. add() 添加元素
s.add(1);
s.add(2);
s.add(3);
s.add(3); // 元素重复: 不能放进去
s.add("a");
// 2. has() 判定是否存在: 存在返回true, 不存在false
console.log(s.has(1)); // true
console.log(s.has(10)); // false
// 3. forEach() 遍历
s.forEach(function (a1, a2, a3) {
console.log(a1, a2, a3);
// 第一个和第二个都是元素值, 第三个是整个集合对象
// 因此: 一般集合的forEach只要1个即可
});
// 4. delete(指定元素) 删除
s.delete("a");
// 5. clear() 清除
s.clear();
// 体现价值: 数组去重
let arr = "jeiworjiowehriohifushiuefhiweuh".split("");
console.log(arr);
// 变成集合: 自动去重
const set = new Set(arr);
console.log(set);
// 将set对象转数组
arr = [...set]; // 打散
console.log(arr);
二:forEach循环
//利用try catch 跳出循环,不能使用return来停止循环
try {
this.dataArray.forEach(item => {
//跳出循环条件
if (item.state == 1) {
this.$message.error("信息提示");
//跳到catch中,执行return跳出循环
throw Error();
}
});
} catch (e) {
return;
}