js笔记
js
js函数
1.console.dir()
查看数据内容。
字符串函数
1.charCodeAt()、fromCharCode()
let str="文字";
console.log(str.charCodeAt(0));//查看unicode
console.log(String.fromCharCode());
25991
文
2.lastIndexOf()、indexOf()
查找子字符串
数组函数
1.push()、pop()、unshift()、shift()
push:向末尾添加,可添加多个,返回值为数组改变后长度。
pop:从队尾删除一个,返回删除的值。
unshift:从前面添加,可添加多个,返回数组改变后的长度。
shift:从前面删除一个,返回删除的值。
2.splice()
删除:1,2之间的值 返回值为删除的值
替换:1,2,‘a’,‘b’ 返回值为删除的值
添加:1,0,‘a’,‘b’,‘c’ 返回值为空
3.sort()
如果提供参数(函数),我们可以指定排序的规则
let num = [6,50,12,40];
num.sort((a,b)=>{return a - b;});// return b - a(倒序)
console.log(num);
·>(4) [6, 12, 40, 50]
随机排序
let num = [6,50,12,40];
num.sort((a,b)=>{return Math.random() - 0.5;});// return b - a(倒序)
console.log(num);
4.forEach()、filter()、map()、reduce()、some()、every()
函数 | 返回值 | 参数 | 功能 |
---|---|---|---|
forEach() | undifined | function(ele,index,arr){},thisArg | 遍历所有元素 |
filter() | function判断为true的元素集合 | function(ele,index,arr){},thisArg | 筛选 |
map() | function处理过的元素集合 | function(ele,index,arr){},thisArg | 处理 |
reduce() | result | function(result,ele,index){},intValue | 计算所有 |
every() | boolean | function(ele,index,arr){},thisArg | 全部符合 |
some() | boolean | function(ele,index,arr){},thisArg | 有一个符合 |
- callback(ele) 函数
- ele :循环过程中的每一位元素
- index: 当前循环的元素对应的下标值
- arr :调用forEach的当前数组
- thisArg: 控制当前callback中的this指向
json
1.parse()、stringify()
let obj = {
name:"key"
}
let _json1 = JSON.stringify(obj);
let _json2='{"key":123}';
console.log(JSON.parse(_json1),JSON.parse(_json2));
·>{name: “key”}·>{key: 123}