文章目录
1. 数字
1. 获取整数位数
let length = 0;
while (num >= 1) {
num /= 10;
length++;
}
2. 获取整数各个位数
const num = 1234;
// 个位
let g = Math.floor(num / Math.pow(10, 0)) % 10;
// 十位
let s = Math.floor(num / Math.pow(10, 1)) % 10;
// 百位
let b = Math.floor(num / Math.pow(10, 2)) % 10;
// 千位
let q = Math.floor(num / Math.pow(10, 3)) % 10;
console.log(g, s, b, q); // 4 3 2 1
若num < 0
,可以先取反,再计算。
3. 字符串转化为数字 parseInt()
第一个参数为要被解析的字符串。
第二个参数表示要解析的数字的基数,可选。
parseInt('100'); // 100
parseInt('19', 10); // 19
parseInt('101101', 2); // 45
parseInt('17', 8); // 15
parseInt('ff', 16); // 255
2. 数组
1. 分隔数组 slice()、splice()
slice()
let abc = [1, 2, 3, 4, 5, 6];
console.log(abc.slice(1, 3)); // [ 2, 3 ] 前闭后开,取下标1,2
console.log(abc.slice(1)); // [ 2, 3, 4, 5, 6 ] 取下标1开始,所有的子数组
console.log(abc.slice(-2)); // [ 5, 6 ] 从倒数第二2个元素开始,取所有的子数组
slice()
方法并不会修改数组,而是返回一个子数组。
splice()
let abc = [1, 2, 3, 4, 5, 6];
console.log(abc.splice(2, 2)); // [ 3, 4 ] 从下标2开始,删除2个,并返回被删除的元素
console.log(abc); // [ 1, 2, 5, 6 ] 改变了原数组
console.log(abc.splice(3, 0, 9)); // 在下标3的位置插入9,后面的元素后移
console.log(abc); // [ 1, 2, 5, 9, 6 ]
splice()
方法会直接对数组进行修改。
2. 数组连接为字符串 join()
let arr = ['a', 'b', 'c'];
console.log(arr.join('')); // abc
console.log(arr.join('-')); // a-b-c
join()
方法将数组连接为字符串。
3. 填充数组元素 fill()
let arr = [1, 2, 3, 4];
console.log(arr.fill(9, 1, 3)); // [ 1, 9, 9, 4 ]
fill()
方法用于将一个固定值替换数组的元素。
参数 | 描述 |
---|---|
value | 必需。填充的值。 |
start | 可选。开始填充位置。 |
end | 可选。停止填充位置 (默认为array.length ) |
3. 字符串
1. 遍历字符串,拿到单个字符
let str = 'abcde';
for (let i = 0; i < str.length; i++) {
console.log(str[i]);
}
2. 查找字符串 indexOf()
let str = 'abcde';
console.log(str.indexOf('b')); // 1
console.log(str.indexOf('g')); // -1
indexOf()
方法可返回某个指定的字符串值在字符串中首次出现的位置。如果要检索的字符串值没有出现,则该方法返回-1
。
3. 分隔字符串 split()
let str = 'abc-de';
let arr1 = str.split('-'); // [ 'abc', 'de' ]
let arr2 = str.split(''); // [ 'a', 'b', 'c', '-', 'd', 'e' ]
split()
方法用于把一个字符串分割成字符串数组。该方法不会改变原字符串。
4. 去除收尾空格 trim()
let str = ' abc ';
let str2 = str.trim();
console.log(str2); // abc
trim()
方法用于删除字符串的头尾空白符,空白符包括:空格、制表符 tab、换行符等其他空白符等。
trim()
方法不会改变原始字符串。
5. 数字转化为字符串 toString()
参数可选,表示数字的基数。若省略该参数,则使用基数 10。
const num = 101;
const str1 = num.toString(); // 101
const str2 = num.toString(2); // 1100101
const str3 = num.toString(8); // 145
6. 获得字符的 Unicode 编码 charCodeAt()
参数必填,表示字符串中某个位置的数字,即字符在字符串中的下标。
const str = 'Jack';
str.charCodeAt(1); // 97
str.charCodeAt(-1); // NaN
str.charCodeAt(99); // NaN
7. Unicode 编码转化为字符 String.fromCharCode()
const char = String.fromCharCode(97);
console.log(char); // a
4. 位运算、运算技巧
1. 数除以 2
将二进制数右移 1 位,相当于将这个数除以 2:
let num1 = 6,
num2 = 7;
num1 = num1 >> 1;
num2 = num2 >> 1;
console.log(num1, num2); // 3 3
而且还舍去了小数,相当于地板除的效果:
let num1 = 6,
num2 = 7;
num1 = Math.floor(num1 / 2);
num2 = Math.floor(num2 / 2);
console.log(num1, num2); // 3 3
2. 去掉二进制最右边的 1
对于二进制数 n:n = n & (n - 1)
可以去掉 n 最右边的一个 1。
let n = 0b1011010;
n = n & (n - 1);
console.log(n.toString(2)); // 1011000
// 把最右边的1去除了
3. 异或
- 0和任何数异或 = 任何数本身
- 任何数和自身异或 = 0
- 异或运算支持交换律、结合律
利用上面三条,得到一堆数据中成单的数:
const nums = [1, 1, 2, 2, 3, 3, 4];
let res = 0;
for (let i = 0; i < nums.length; i++) {
res ^= nums[i];
}
console.log(res); // 4
5. 其他
1. 判断两个值是否为同一个值 Object.is()
Object.is('foo', 'foo'); // true
Object.is('foo', 'bar'); // false
Object.is([], []); // false
const foo = { a: 1 };
const bar = { a: 1 };
Object.is(foo, foo); // true
Object.is(foo, bar); // false
Object.is(null, null); // true
// 特例
Object.is(0, -0); // false
Object.is(0, +0); // true
Object.is(-0, -0); // true
Object.is(NaN, 0 / 0); // true
📘📘欢迎在我的博客上访问:
https://lzxjack.top/