typeof
typeof null === "object"
function fn() {}
typeof fn === "function"
instanceof
[] instanceof Array; // true 重要!!!! 原理: [].__proto__ === Array.prototype
function ff() {}
ff instanceof Function // true
ff instanceof Object // true
indexOf
str.indexof(c);
未找到则返回-1
'fdsfsd'.indexOf('z') // -1
'fdsfsd'.indexOf('d') // 1
charAt
charAt(index) index表示下标 整个表示取字符串第index个字符
'fsdfsadfs'.charAt('0') === "f"
'abc'.charAt(2) === "c"
ACSII
A - 65 Z - 90
a - 97 z - 122
'a'.charCodeAt(); // 97
String.fromCharCode(97); // "a"
stringObj.replace (不会破坏原字符串,返回替换后的字符串)
MDN
str.replace(regexp|substr, newSubStr|function)
'fewfewf'.replace('f', 'zz') // "zzewfewf" 仅仅第一个匹配到的会被替换掉
'fewfewf'.replace(/f/, '') // "ewfewf" 仅仅第一个匹配到的会被替换掉
'fewfewf'.replace(/f/g, '') // "ewew" 替换了所有的f
// 电话号码转 ‘*’
const str = 'fdfjlsd-17712894696-dj-5-fglds-3-fj';
const reg = /1\d{10}/g;
const newStr = str.replace(reg, function (w) {
let s = ''
const len = w.length;
for (let i = 0; i < len; i++) {
s += '*'
}
return s;
});
console.log(str);
console.log(newStr);
const buyLinkReg = new RegExp(`^https?:\/\/${API_ORIGIN_NO_PROTOCOL}\/openrack\/(\\w{8}(-\\w{4}){3}-\\w{12}?)\/buy$`, '');
if (buyLinkReg.test(value)) {
this.setState({
boxNo: value.trim().replace(buyLinkReg, '$1')
});
}
delete
删除Object中的某个key,返回值是true or false
delete params['currentPage']
for of
for of不仅能遍历数组,也能遍历字符串
Array.reverse()
[1, 2, 3] => [3, 2, 1]
按位异或运算符 ^
例如:10100001^00010001=10110000
0 ^ 0=0,0^1=1 0异或任何数=任何数
1 ^ 0=1,1^1=0 1异或任何数-任何数取反
任何数异或自己=把自己置0
0->1 1->0
1- n就可以
unshift
在数组首位置添加元素
注意这个方法最容易出错的地方是返回值是长度,不是返回一个新数组!!!把他想成跟push,pop一样就可以了。
var arr = [];
let length = arr.unshift(1,2);
console.log(arr); // [1,2]
console.log(length); // 2
console.log(arr.length); // 2
length = arr.unshift(3,4);
console.log(arr); // [,3,4,1,2]
console.log(length); // 4
console.log(arr.length); // 4
shift
删除数组首位置元素
const a = arr.shift();
console.log(a); // 3
console.log(arr) // [4, 1, 2]
concat 返回的是一个新数组,不会改变原来的数组,跟push pop不一样
var a = [1 ,2 ,3 ,4 ,5 ];
undefined
var b = a.concat([6, 7, 8])
undefined
b
(8) [1, 2, 3, 4, 5, 6, 7, 8]
a
(5) [1, 2, 3, 4, 5]
Array.from(new Set(array)) || [...new Set(array)]
Math.max && Math.min
Math.max(1, 2, 3)
3
Math.max.apply(null, [1, 2, 3])
3
Array.map
// 返回的是skuIds数组
select = rowArr.map(item => {
return item.skuId;
})
const arr = [1, 2, 3];
const doubleArr = arr.map(item => item * 2); // [2, 4, 6];
or
const doubleArr = arr.map(item => {
return item * 2;
})
some
const checkArr = function(ele) {
console.log(ele);
if (ele > 5) {
return true;
} else {
return false;
}
}
const arr = [1, 2, 4, 6, 7];
console.log(arr.some(checkArr));
output: 1, 2, 4, 6 true
slice数组或者字符串
slice不会改变原来数组
const arr = [1, 2, 3 ,4 ,5];
const newArr = arr.slice(0, 3);
console.log(newArr); // [1, 2, 3]
console.log(arr); // [1, 2, 3, 4, 5]
var arr = [1, 2, 3 ,4 ,5];
var newArr = arr.slice(0, -1); // -1表示到倒数第二个元素
// 如果下标不合法,比如 (3, -2), 则会返回一个空数组
console.log(newArr); // [1, 2, 3]
console.log(arr); // [1, 2, 3, 4, 5]
slice数组或者字符串
var str = 'The quick red fox jumped over the lazy dog\'s back.';
console.log(str.slice(30)); // expected output: "the lazy dog's back."
console.log(str.slice(4, 17)); // expected output: "quick red fox"
console.log(str.slice(-5)); // expected output: "back."
console.log(str.slice(-11, -6)); // expected output: "dog's"
splitstring函数
var str="How are you doing today?"
console.log(str.split(" ")); // ["How", "are", "you", "doing", "today?"]
console.log(str.split("")); // ["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", " ", "d", "o", "i", "n", "g", " ", "t", "o", "d", "a", "y", "?"]
console.log(str.split(" ",3)); // ["How", "are", "you"]
splice数组函数
splice MDN
注意splice会改变原来的数组
const arr = [1, 2, 3 ,4 ,5];
const newArr = arr.splice(0, 3);
console.log(newArr); // [1, 2, 3]
console.log(arr); // [4, 5]