字符串及数组的一些常用方法!!!!

一、数组

1、检测是不是数组:isArray()

在es6中新增了Array.isArray()方法,用来确认某个值是不是数组。

  var array=[1,2,3,4,5]
  var str='123'
   console.log(Array.isArray(array)) //true
   console.log(Array.isArray(str)) //false

2、转换方法:toString()、join()

调用数组的toString()方法会返回由数组中的每个值的字符串形式凭借而成的以逗号分隔的字符串。例如:

  var array=[1,2,3,4,5]
  console.log(array.toString())//1,2,3,4,5

使用join()方法可以使用不同的分隔符来将数组内的值拼接成字符串的形式。join()方法接收一个参数,即用作分隔符的字符串。

  var array=[1,2,3,4,5]
  console.log(array.join('-'))//1-2-3-4-5

3、Push() 添加到最后 返回添加后的数组长度;

  var array=[1,2,3,4,5]
  console.log(array.push('6'))//6

4、Pop() 删除最后一项 返回处理后的数组

  var array=[1,2,3,4,5]
  console.log(array.pop())//5

5、Unshift()和unshift()

Shift() 删除(从前面删除)返回处理后的数组;

  var array=[1,2,3,4,5]
  console.log(array.shift())//1
  console.log(array)// 2, 3, 4, 5

Unshift() 添加到最前面 返回添加后的数组长度;

  var array=[1,2,3,4,5]
  console.log(array.unshift('65'))//6
  console.log(array)//"65", 1, 2, 3, 4, 5

5、排序:reverse()、sort()

reverse()方法会反转数组项的顺序。例如:

  var array=[1,2,3,4,5]
  console.log(array.reverse())//5, 4, 3, 2, 1
  console.log(array)// 5, 4, 3, 2, 1

sort()排序

  var array=[6,2,8,11,5]
  console.log(array.sort((a,b)=>{return a-b}))//2, 5, 6, 8, 11 正序
  console.log(array.sort((a,b)=>{return b-a}))//11, 8, 6, 5, 2 反序

6、slice(start,end) 截取数组中的从start(开始)到end(结束 不包含) 返回新数组,原数组不变

  var array=[6,2,8,11,5]
  var arr=array.slice(2,4)
  console.log(arr)//8, 11
  console.log(array)//6,2,8,11,5

7、concat() 方法用于连接两个或多个数组。

先创建当前数组一个副本,然后将接收到的参数添加到这个副本的末尾,最后返回一个新构建的数组。不会改变原数组的项。

  var array=[6,2,8,11,5]
  var arr=[10,20]
  arr=arr.concat(array)
  console.log(arr)//10, 20, 6, 2, 8, 11, 5
  console.log(array)//6,2,8,11,5

8、splice()可以对数组项进行删除、替换、插入操


//删除
  var array=[6,2,8,11,5]
  var removed=array.splice(0,1)
  console.log(removed)//6 所删除的项
  console.log(array)//2,8,11,5 删除之后的数组

//插入
removed = array.splice(1, 0, "20", "30");
 console.log(array); //2, "20", "30", 8, 11, 5 插入之后的数组
 console.log(removed); //[]

//替换
removed = array.splice(1, 1, "50", "60");
console.log(array); //2, "50", "60", "30", 8, 11, 5 替换之后的数组
console.log(removed); //"20" 被替换的一项

9、位置方法:indexOf()、lastIndexOf()

这两个方法都可以接收两个参数:要查找的项和表示查找起点位置的索引(可选)。返回值是要查找的项在数组中的位置,如果没有找到,则返回-1。
indexOf():从前向后查找
lastIndexOf():从后向前查找

  var array=[6,2,8,11,5]
  console.log(array.indexOf(11))//3
  console.log(array.indexOf(20))//-1
  console.log(array.indexOf(2,11))//-1
  console.log(array.lastIndexOf(2,11))//1
  console.log(array.lastIndexOf(11))//3
  console.log(array.lastIndexOf(20))//-1

10、迭代方法:every()、some()、filter()、forEach()、map()

every()和some()都用于查询数组中的项是否满足某个条件,every()是当传入函数的每一项都符合时返回true,而some()是只要有一项满足就返回true。

  var array = [6, 2, 8, 11, 5]
  var everyResult = array.every(function (item, index, array) {
    return (item > 2);
  });
  console.log(everyResult); //false

  var someResult = array.some(function (item, index, array) {
    return (item > 2);
  });
  console.log(someResult); //true

filter()返回数组中符合条件的项的数组。

  var array = [6, 2, 8, 11, 5]
 var filterResult = array.filter(function(item, index, array) {
 return (item > 2);
 });
  console.log(filterResult); //6, 8, 11, 5

map()返回在原始函数每一项上运行传入函数的结果的数组。

  var array = [6, 2, 8, 11, 5]
 var mapResult = array.map(function(item, index, array) {
 return (item * 2);
 });
  console.log(mapResult); //12, 4, 16, 22, 10

forEach()对数组中的每一项运行传入的函数,没有返回值。

 var array = [6, 2, 8, 11, 5]
 array.forEach(function(item, index, array) {
 
 });

二、字符串

1、substring(start开始位置索引,end结束位置索引)截取的位置不包含结束位置的字符,只写一个参数表示重开始位置截取最后

   var str='hello 寒冷东'
  console.log(str.substring(5,9))// 寒冷东
  console.log(str.substring(5))// 寒冷东 从第五个之最后
  console.log(str.substring(5,-1))// hello 输入负值变为0,那个最小从那个开始

2、slice(start开始位置索引,end结束位置索引)基本和substring相似,区别在参数为负数

  var str='hello 寒冷东'
  console.log(str.slice(5,9))// 寒冷东
  console.log(str.slice(5))// 寒冷东 从第五个之最后
  console.log(str.slice(5,-1))// 寒冷 从后面倒数第一个算起
  console.log(str.slice(-5))// o 寒冷东 从后面倒数第5个算起
  console.log(str.slice(-1,-3))// ' ' 当第二个参数时大于第一个参数时返回 空

3、substr(start开始位置索引,end需要返回字符个数)

 var str='hello 寒冷东'
  console.log(str.substr(3,4))// lo 寒 从第三个开始,后面的4个字符串
  console.log(str.substr(3))// lo 寒冷东 从第三个开始后面所有的字符串
  console.log(str.substr(-2))// 冷东  从倒数第二个开始后面所有的字符串
  console.log(str.substr(-2,-5))// ' ' 如果结束参数为负数的时候,返回空

4、charAt(index)方法返回指定索引位置处的字符。

  var str='hello 寒冷东'
  console.log(str.charAt(2))// l 返回相对应的字符串
  console.log(str.substr(18))// ' ' 如果超出字符串长度则返回空

5、indexOf()和lastIndexOf()

与数组中的位置方法相同。这两个方法都是从一个字符串中搜索给定的子字符串,然后返回子字符串的位置,如果没有找到,则返回-1。

  var str='hello 寒冷东'
  console.log(str.indexOf('寒'))// 6
  console.log(str.indexOf('a'))// -1
  console.log(str.lastIndexOf('o'))// 4
  console.log(str.lastIndexOf('a'))// -1

6、split()将字符串以参数分割为数组

  var str='hello 寒冷东'
  console.log(str.split(''))// ["h", "e", "l", "l", "o", " ", "寒", "冷", "东"]

7、大小写转换:toLowerCase()和toUpperCase()

  var str='hello 寒冷东'
  console.log(str.toUpperCase())// HELLO 寒冷东
  console.log(str.toLowerCase())// hello 寒冷东

8、trim()

此方法回去除字符串前后所有的空白字段,中间的不会去除

 var str='         hello 寒冷东          '
  console.log(str.length)//28
  
  console.log(str.trim().length)// 9

9、匹配方法:match()、search()、replace()

match()-方法可在字符串内检索指定值,或找到一个或多个正则表达式的匹配

var str="1 plus 2 equal 3"
console.log(str.match(/\d+/g))//["1", "2", "3"]

search()方法返回与正则表达式查找内容的第一个字符串的位置。

var str='hello 寒冷东'
console.log(str.search('寒冷东'))//6

replace()用来查找匹配一个正则表达式的字符串,然后使用新字符串代替匹配;

var str='hello 寒冷东'
var result=str.replace('l','oo')
console.log(result)//heoolo 寒冷东
var result2=str.replace(/l/g,'lxd')
console.log(result2)//helxdlxdo 寒冷东

以上就是小弟总结的一些字符串和数组的方法,各位大佬如果觉得有用点个赞 支持一下,谢谢。

猜你喜欢

转载自blog.csdn.net/weixin_42870180/article/details/89307904