JavaScript都得知道这3个数组方法


JavaScript都得知道这3个数组方法

文章插图
在该文中,将介绍以下三种方法:
1. join()方法
2. split()方法
3. sort()方法
为什么每个JAVAScript开发人员都要知道这些方法?因为数组是代码中的重要元素,而这些方法可以让代码更优雅和更具代表性 。
在没有这些方法的情况下也可以运行项目,但为此必须编写不必要的代码行,而这些代码行原先就没有用处 。
那就开始吧,首先了解一下 join() 和 split() 这两种基本的方法,再讨论 sort() 方法 。
 
 
1. Join()方法想象一下这样的场景:用户在数组中输入一些值,然后想把这些值看作消息或字符串 。
这就需要用到 join() 方法,它可以把数组中的元素转换成字符串 。
toString() 也用于将字符串转换为数组,但是采用 join() 方法,则可以使用separator参数,因此最好使用 join()方法 。
join()语法很简单,只需使用:
array.join(separator)
此处separator在传递参数中是可选的,用于定义数组中想要分隔的元素,可以是空格、圆点、逗号和单词等 。
如果没有传递参数,则其元素需用逗号分隔 。
看一个实例:
const array1=[1,2,3,'My','Name','is','Ney'] const string1=array1.join() const string2=array1.join('') const string3=array1.join(',') const string4=array1.join('and') const string5=array1.join('-') const string6=array1.join('=') const string7=array1.join(':') const string8=array1.join(' ') console.log(array1) // [ 1, 2, 3, 'My', 'Name', 'is', 'Ney' ] console.log(string1) // 1,2,3,My,Name,is,Ney console.log(string2) //123MyNameisNey console.log(string3) // 1,2,3,My,Name,is,Ney console.log(string4) // 1and2and3andMyandNameandisandNey console.log(string5) // 1-2-3-My-Name-is-Ney console.log(string6) // 1=2=3=My=Name=is=Ney console.log(string7) // 1:2:3:My:Name:is:Ney console.log(string8) // 1 2 3 My Name is Ney
上面举了好几个例子,其中要重点讨论的是 string8 和 string2 。
在 string2中,引号之间没有任何空格,而在 string8中它则有空格 。
可以在引号中放置任意数量的空格,而结果也会随之改变 。
 
2. Split()方法 
因此,我们已经知道数组中的元素可以转换为字符串 。
可以把数组中的字符串转换为元素吗?这就是 split() 方法的用处 。
split() 方法在如下场景中使用起来十分方便,即必须输入消息并查看其中是否包含特定的单词 。使用 includes() 方法可以通过把单词转换成数组,轻松地实现这一目的 。下文很快会提及 。
在把字符串转换为数组后,仍然可以执行其他的许多功能 。从技术角度看, split() 是一种字符串方法,但我会此处有所提及 。
首先看一下 split() 的语法:
string.split(separator, limit)
· Separator指定用于拆分字符串的字符 。如果留有空格,则整个字符串将转换为数组中的单个元素 。
· Limit是一个可选参数,很少使用 。它是一个整数,指定拆分的数量 。Limit拆分后的项目不会包含在该数组中 。
看一些实例:
这里会使用上文提到的 join() 方法例子,并用 split() 方法转换成字符串 。
const string1 = `1,2,3,My,Name,is,Ney` const array1 = string1.split(',') const arrayWithLimit = string1.split(',', 4) const arrayWithoutSeperator = string1.split() console.log(array1, arrayWithLimit, arrayWithoutSeperator) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] [ '1', '2', '3', 'My' ] [ '1,2,3,My,Name,is,Ney' ] const string2 = `123MyNameisNey` const array2 = string2.split('') console.log(array2) //[ '1', ',', '2', ',', '3', ',', 'M', 'y', ',', 'N', 'a', 'm', 'e', ',', 'i', 's', ',', 'N', 'e', 'y' ] const string3 = `1,2,3,My,Name,is,Ney` const array3 = string3.split(',') console.log(array3) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] const string4 = `1and2and3andMyandNameandisandNey` const array4 = string4.split('and') console.log(array4) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] const string5 = `1-2-3-My-Name-is-Ney` const array5 = string5.split('-') console.log(array5) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] const string6 = `1=2=3=My=Name=is=Ney` const array6 = string.split('=') console.log(array6) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] const string7 = `1:2:3:My:Name:is:Ney` const array7 = string7.split(':') console.log(array7) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ] const string8 = `1 2 3 My Name is Ney` const array8 = string8.split(' ') console.log(array8) //[ '1', '2', '3', 'My', 'Name', 'is', 'Ney' ]


推荐阅读