常见操作,异常汇总
1.判断某字符串是否包含某子字符串
str.indexOf("子字符串") != -1
// eg
var str = "hello world"
str.indexOf("hello") != -1; // --> true
2.Vscode快捷生成标签
/*
> : 父子关系
[]: 属性
$ : 升序生成数字
+ : 并列关系
.#: 类名 id名
lorem: 随机生成文本
*/
(li>img[src="./img/$.png"]+p.name)*5
3.解决a标签锚点偏移
给锚点元素添加一个偏移
<div id="_id" style="position: relative;top: -60px;"></div>
4.展开数组
flat()
const ls = [[1,2,3],[4,5,6]]
console.log(ls.flat()) // [1, 2, 3, 4, 5, 6]
5.创建一个二维数组
// n*n的矩阵
const ls = new Array(n).fill(0).map(()=> new Array(n).fill(0));
// m*n的矩阵
const ls = new Array(m).fill(0).map(()=> new Array(n).fill(0));
6.while循环的区别
let l = 0
let r = 10
while(++l < --r){
console.log(l,r);
}
// 1 9
// 2 8
// 3 7
// 4 6
let left = 0
let right = 10
while (left < right){
console.log(left,right);
left ++
right --
}
// 0 10
// 1 9
// 2 8
// 3 7
// 4 6
7.sort排序数字
const ls = [2,1,3,1]
ls.sort((a,b)=>a-b)