文章目录
String
String(thing)
字符串化
str.length
返回字符串长度
str.toUpperCase()
转换成大写
str.toLowerCasw()
转换成小写
换行字符串
使用 + 运算符将多个字符串连接
每行末尾使用反斜杠字符 \
let longString = "This is a very long string which needs " +
"to wrap across multiple lines because " +
"otherwise my code is unreadable.";
let longString = "This is a very long string which needs \
to wrap across multiple lines because \
otherwise my code is unreadable.";
从字符串中获取单个字符
str.charAt(n)
把字符串当作一个类似数组的对象,其中的每个字符对应一个数值索引
'cat'.charAt(1);
'cat'[1];
字符串比较
比较操作符(>/</>=/<=) fanhui
基本字符串和字符串对象的区别
console.log(typeof 'foo'); // Logs "string"
console.log(typeof new String('foo')); // Logs "object"
使用 eval 时,基本字符串和字符串对象也会产生不同的结果。
eval 会将基本字符串作为源代码处理;
字符串对象则被看作对象处理, 返回对象
利用valueOf方法,我们可以将 字符串对象 转换为对应的 基本字符串
var s1 = '2 + 2'; // creates a string primitive
var s2 = new String('2 + 2'); // creates a String object
console.log(eval(s1)); // returns the number 4
console.log(eval(s2)); // returns the string "2 + 2"
console.log(eval(s2.valueOf())); // returns the number 4
- 拼接
str.concat()
- 包含 / 结尾包含
str.includes()
str.endWith()
- 查找
str.indexOf("x") /lastIndexOf()
查找str返回x首次在str中的位置/如果没找到返回-1
str.search()
查找字符返回当前索引位
match()
查找指定字符串,找到返回该字符串/没有返回null - 截取返回字符串
str.slice(startnum,endnum) /substring(startnum,toend)
[startnum,toend时省略)
str.substr(start,length)
start开始截取位置,length表示截取长度 - 字符串替换内容
str.replace("被替换内容","替换的新内容")
- 分割
str.split("分隔符号")
- 字符串(数组转化以“,”拼接)
arr.toString()
- 去空白
str.trim() trimRight() trimLeft()
去除字符串两端/前段/后端空白
Array
every
检测数组所有元素符合条件 返回true /false
var boo=arr.every(function(currentValue, index){
return val>0;
});//true /false
forEach
代替for循环
调用数组的每个元素,并将元素传递给回调函数–空数组不执行
array.forEach(function(currentValue, index, arr){
//currentValue 必需--当前元素
//index 可选--当前元素的索引值。
//arr 可选--当前元素所属的数组对象。
//thisValue可选--传递给函数的值一般用 "this" 值。 如果这个参数为空--"undefined" 会传递给 "this" 值
},thisValue)
map
映射新数组
map() 方法返回新数组,数组中的元素为原始数组元素调用函数处理后的值。- 不改变原始数组- 不会对空数组进行检测
new Array=array.map(function(currentValue, index, arr),thisValue);
reduce
累计
new Array=str.reduce(function(total, currentvaluel, currentindex,arr){
return val1+val2;
},initialvalue)
array.some(function(currentValue,index,arr),thisValue)
检测数组中的元素是否满足指定条件
有一个元素满足条件返回true , 剩余的元素不再执行//若没有满足条件的元素返回false
注意: some() 不会对空数组进行检测。
数组的增加、修改、删除splice
pop() //删除数组最后一个元素 ``push()向数组末尾添加一个元素
shift() 删除数组第一个元素 ``unshift() 向数组开头添加一个元素
- 添加/删除指定位置(改变原数组)
array.splice(index,howmany,item1,.....,itemX)
howmany=0表示插入,other为删除个数
数组的截取slice和拼接contact
arr.slice(start, end)
从索引n开始,一直到索引m(不含m),将找到的部分数组返回,原来的数组不变
arr.slice(n)
从索引n开始找一直找到数组末尾
arr.slice(0)
把原来的参数克隆一模一样的
arr1.concat(arr2)
合并连接arr1和arr2,原来的数组不变
arr.concat()
把arr克隆一份一模一样的数组
数组转换为字符串join toString
arr.toString()
把数组转换为字符串,原来的数组不变
arr.join('+')
把数组按照指定的分隔符拼接成字符串,原来的数组不变
eval()
js中把字符串变成js表达式执行的一个方法
排序sort和排列
arr.reverse()
把数组倒过来排序,原来的数组改变
arr.sort()
按照数组的第一位从小到大排序(只能处理10以内的数组进行排序)
查找find indexOf 过滤查找fillter
arr.indexOf(str)/lastIndexOf()
字符串在数组中出现的位置(索引值),没找到返回-1
find
返回满足第一个条件值
arr.find(function(val,index,){
return val>9;})
fillter
过滤满足条件元素
new Array=arr.filter(function(val,index,arr){
if(val>9){
return val;}
})
替换fill
fill
固定值替换 –原数组收影响
arr.fill(替换内容,start,end)
reduce()函数累加器
reduce() 方法接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值。
array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
参数 | 描述 |
---|---|
total 必需。 | 初始值, 或者计算结束后的返回值。 |
currentValue 必需。 | 当前元素 |
currentIndex 可选。 | 当前元素的索引 |
arr 可选。 | 当前元素所属的数组对象。 |
initialValue 可选。 | 传递给函数total的初始值 |
用reduce将数组对象转化成数组
var arr = [
{name:"zhao",age:20},
{name:"qian", age:30}
];
//{ zhao: { name: ‘zhao’, age: 20 }, qian: { name: ‘qian’, age: 30 } }
var res=arr.reduce(function(total,currv){
total[currv.name] = currv
return total;
},{});