加入收藏 | 设为首页 | 会员中心 | 我要投稿 莆田站长网 (https://www.0594zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > 建站资源 > 优化 > 正文

JavaScript数组方法三板斧,100%的开发都得知道

发布时间:2019-08-14 23:07:34 所属栏目:优化 来源:读芯术
导读:副标题#e# 在该文中,将介绍以下三种方法: 1. join()方法 2. split()方法 3. sort()方法 为什么每个JavaScript开发人员都要知道这些方法?因为数组是代码中的重要元素,而这些方法可以让代码更优雅和更具代表性。 在没有这些方法的情况下也可以运行项目,
副标题[/!--empirenews.page--]

JavaScript数组方法三板斧,100%的开发都得知道

在该文中,将介绍以下三种方法:

1. join()方法

2. split()方法

3. sort()方法

为什么每个JavaScript开发人员都要知道这些方法?因为数组是代码中的重要元素,而这些方法可以让代码更优雅和更具代表性。

在没有这些方法的情况下也可以运行项目,但为此必须编写不必要的代码行,而这些代码行原先就没有用处。

那就开始吧,首先了解一下 join() 和 split() 这两种基本的方法,再讨论 sort() 方法。

1. Join()方法

想象一下这样的场景:用户在数组中输入一些值,然后想把这些值看作消息或字符串。

这就需要用到 join() 方法,它可以把数组中的元素转换成字符串。

toString() 也用于将字符串转换为数组,但是采用 join() 方法,则可以使用separator参数,因此最好使用 join()方法。

join()语法很简单,只需使用:

  1. array.join(separator) 

此处separator在传递参数中是可选的,用于定义数组中想要分隔的元素,可以是空格、圆点、逗号和单词等。

如果没有传递参数,则其元素需用逗号分隔。

看一个实例:

  1. const array1=[1,2,3,'My','Name','is','Ney'] 
  2. const string1=array1.join() 
  3.  
  4. const string2=array1.join('') 
  5.  
  6. const string3=array1.join(',') 
  7.  
  8. const string4=array1.join('and') 
  9.  
  10. const string5=array1.join('-') 
  11.  
  12. const string6=array1.join('=') 
  13.  
  14. const string7=array1.join(':') 
  15.  
  16. const string8=array1.join(' ') 
  17.  
  18.  
  19. console.log(array1) 
  20. //  [ 1, 2, 3, 'My', 'Name', 'is', 'Ney' ] 
  21.  
  22. console.log(string1) 
  23. // 1,2,3,My,Name,is,Ney 
  24.  
  25. console.log(string2) 
  26. //123MyNameisNey 
  27.  
  28. console.log(string3) 
  29. // 1,2,3,My,Name,is,Ney 
  30.  
  31. console.log(string4) 
  32. // 1and2and3andMyandNameandisandNey 
  33.  
  34. console.log(string5) 
  35. // 1-2-3-My-Name-is-Ney 
  36.  
  37. console.log(string6) 
  38. // 1=2=3=My=Name=is=Ney 
  39.  
  40. console.log(string7) 
  41. // 1:2:3:My:Name:is:Ney 
  42.  
  43. console.log(string8) 
  44. // 1 2 3 My Name is Ney 

上面举了好几个例子,其中要重点讨论的是 string8 和 string2。

在 string2中,引号之间没有任何空格,而在 string8中它则有空格。

可以在引号中放置任意数量的空格,而结果也会随之改变。

2. Split()方法

因此,我们已经知道数组中的元素可以转换为字符串。

可以把数组中的字符串转换为元素吗?这就是 split() 方法的用处。

split() 方法在如下场景中使用起来十分方便,即必须输入消息并查看其中是否包含特定的单词。使用 includes() 方法可以通过把单词转换成数组,轻松地实现这一目的。下文很快会提及。

在把字符串转换为数组后,仍然可以执行其他的许多功能。从技术角度看, split() 是一种字符串方法,但我会此处有所提及。

首先看一下 split() 的语法:

  1. string.split(separator, limit) 
  • Separator指定用于拆分字符串的字符。如果留有空格,则整个字符串将转换为数组中的单个元素。
  • Limit是一个可选参数,很少使用。它是一个整数,指定拆分的数量。Limit拆分后的项目不会包含在该数组中。

看一些实例:

这里会使用上文提到的 join() 方法例子,并用 split() 方法转换成字符串。

  1. const string1 = `1,2,3,My,Name,is,Ney` 
  2.  
  3. const array1 = string1.split(',') 
  4. const arrayWithLimit = string1.split(',', 4) 
  5. const arrayWithoutSeperator = string1.split() 
  6.  
  7. console.log(array1, arrayWithLimit, arrayWithoutSeperator) 
  8. //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] [ '1', '2', '3', 'My' ] [ '1,2,3,My,Name,is,Ney' ] 
  9.  
  10.  
  11.  
  12. const string2 = `123MyNameisNey` 
  13. const array2 = string2.split('') 
  14. console.log(array2)                                        
  15. //[ '1',  ',',  '2',  ',',  '3',  ',',  'M',  'y',  ',',  'N',  'a',  'm',  'e',  ',',  'i',  's',  ',',  'N',  'e', 'y' ] 
  16.  
  17. const string3 = `1,2,3,My,Name,is,Ney` 
  18. const array3 = string3.split(',') 
  19. console.log(array3)                                    //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] 
  20.  
  21.  
  22. const string4 = `1and2and3andMyandNameandisandNey` 
  23. const array4 = string4.split('and') 
  24. console.log(array4)                                      //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] 
  25.  
  26.  
  27. const string5 = `1-2-3-My-Name-is-Ney` 
  28. const array5 = string5.split('-') 
  29. console.log(array5)                                      //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] 
  30.  
  31.  
  32. const string6 = `1=2=3=My=Name=is=Ney` 
  33. const array6 = string.split('=') 
  34. console.log(array6)                                      //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] 
  35.  
  36.  
  37. const string7 = `1:2:3:My:Name:is:Ney` 
  38. const array7 = string7.split(':') 
  39. console.log(array7)                                      //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] 
  40.  
  41.  
  42. const string8 = `1 2 3 My Name is Ney` 
  43. const array8 = string8.split(' ') 
  44. console.log(array8)                                      //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] 

(编辑:莆田站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读