最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

javascript中數(shù)組和字符串的方法對(duì)比

 更新時(shí)間:2016年07月20日 08:49:47   投稿:jingxian  
下面小編就為大家?guī)硪黄猨avascript中數(shù)組和字符串的方法對(duì)比。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

前面的話

字符串和數(shù)組有很多的相同之處,它們的方法眾多,且相似度很高;但它們又有不同之處,字符串是不可變值,于是可以把其看作只讀的數(shù)組。本文將對(duì)字符串和數(shù)組的類似方法進(jìn)行比較

可索引

ECMAScript5定義了一種訪問字符的方法,使用方括號(hào)加數(shù)字索引來訪問字符串中的特定字符

可索引的字符串的最大的好處就是簡單,用方括號(hào)代替了charAt()調(diào)用,這樣更加簡潔、可讀并且可能更高效。不僅如此,字符串的行為類似于數(shù)組的事實(shí)使得通用的數(shù)組方法可以應(yīng)用到字符串上

如果參數(shù)超出范圍或是NaN時(shí),則輸出undefined

var str = "hello";
console.log(str[0]);//h
console.log(str[[1]]);//e

console.log(str[false]);//undefined
console.log(str[-1]);//undefined
console.log(str[NaN]);//undefined
console.log(str[]);//報(bào)錯(cuò)
var arr = ['h','e','l','l','o'];
console.log(arr[0]);//h
console.log(arr[[1]]);//e

console.log(arr[false]);//undefined
console.log(arr[-1]);//undefined
console.log(arr[NaN]);//undefined
console.log(arr[]);//報(bào)錯(cuò)

轉(zhuǎn)換

字符串可以使用split()方法轉(zhuǎn)換為數(shù)組;而數(shù)組可以使用join()方法轉(zhuǎn)換為字符串

【split()】

split()方法基于指定的分隔符將一個(gè)字符串分割成多個(gè)字符串,并將結(jié)果放在一個(gè)數(shù)組中,分隔符可以是字符串,也可以是一個(gè)正則表達(dá)式

該方法可以接受(可選的)第二個(gè)參數(shù)用于指定數(shù)組的大小。如果第二個(gè)參數(shù)為0-array.length范圍內(nèi)的值時(shí),按照指定參數(shù)輸出,其他情況將所有結(jié)果都輸出

若指定分隔符沒有出現(xiàn)在字符串中,則以數(shù)組的形式返回原字符串的值

var colorText = 'red,blue,green,yellow';
console.log(colorText.split(''));//["r", "e", "d", ",", "b", "l", "u", "e", ",", "g", "r", "e", "e", "n", ",", "y", "e", "l", "l", "o", "w"]
console.log(colorText.split(','));//["red", "blue", "green", "yellow"]
console.log(colorText.split(',',2));//["red", "blue"]
console.log(colorText.split(',',6));//["red", "blue", "green", "yellow"]
console.log(colorText.split('-'));//["red,blue,green,yellow"]
console.log(colorText.split(/\,/));//["red", "blue", "green", "yellow"]
console.log(colorText.split(/e/));//["r", "d,blu", ",gr", "", "n,y", "llow"]
console.log(colorText.split(/[^\,]+/));//將除去逗號(hào)以外的字符串變?yōu)榉指舴鸞"", ",", ",", ",", ""],IE8-會(huì)識(shí)別為[",",",",","]

【join()】

join()方法可以使用不同的分隔符來構(gòu)建這個(gè)字符串,join()方法只接收一個(gè)參數(shù),用作分隔符的字符串,然后返回包含所有數(shù)組項(xiàng)的字符串

如果不給join()方法傳入任何值,則使用逗號(hào)作為分隔符

var a = [1,2,3];
console.log(a.join());//'1,2,3'
console.log(a.join(' '));//'1 2 3'
console.log(a.join(''));//'123'

var b = new Array(10);
b.join('-');//'---------',9個(gè)連字符組成的字符串

如果數(shù)組中的某一項(xiàng)的值是null或者undefined,則該值在join()方法返回的結(jié)果中以空字符串表示

var colors = [1,undefined,2,null,3];
console.log(colors.join());//'1,,2,,3'

由于字符串是類數(shù)組對(duì)象,所以,也可以使用join()方法

console.log(Array.prototype.join.call('hello', '-'));// "h-e-l-l-o"
var str = 'test';
var arr = str.split('')//["t", "e", "s", "t"]
console.log(arr.join('-'));//'t-e-s-t'

拼接  

字符串和數(shù)組共同擁有拼接方法concat()

var value = 'hello';
console.log(value.concat('world'));//'helloworld'
console.log(value.concat(['world']));//'helloworld'
console.log(value.concat([['world']]));//'helloworld'
var value = ['hello'];
console.log(value.concat('world'));//["hello", "world"]
console.log(value.concat(['world']));//["hello", "world"]
console.log(value.concat([['world']]));//["hello", ["world"]]

創(chuàng)建

字符串和數(shù)組都擁有創(chuàng)建方法slice(),分別用于創(chuàng)建子字符串和子數(shù)組

slice()方法基于當(dāng)前數(shù)組(或字符串)中的一個(gè)或多個(gè)項(xiàng)創(chuàng)建一個(gè)新數(shù)組(或字符串),接受一個(gè)或兩個(gè)參數(shù),即要返回項(xiàng)的起始和結(jié)束位置,最后返回新數(shù)組(或字符串)

slice(start,end)方法需要兩個(gè)參數(shù)start和end,返回這個(gè)數(shù)組(或字符串)中從start位置到(但不包含)end位置的一個(gè)子數(shù)組(或字符串);如果end為undefined或不存在,則返回從start位置到數(shù)組(或字符串)結(jié)尾的所有項(xiàng)

如果start是負(fù)數(shù),則start = max(length + start,0)

如果end是負(fù)數(shù),則end = max(length + end,0)

start和end無法交換位置

var numbers = [1,2,3,4,5];
console.log(numbers.slice(2));//[3,4,5]
console.log(numbers.slice(2,undefined));//[3,4,5]
console.log(numbers.slice(2,3));//[3]
console.log(numbers.slice(2,1));//[]

console.log(numbers.slice(-3));//-3+5=2 -> [3,4,5]
console.log(numbers.slice(-8));//max(5 + -8,0)=0 -> [1,2,3,4,5]

console.log(numbers.slice(0,-3));//-3+5=2 -> [1,2]
console.log(numbers.slice(-2,-1));//-2+5=3;-1+5=4; -> [4]
var stringValue = 'hello world';
console.log(stringValue.slice());//'hello world'
console.log(stringValue.slice(2));//'llo world'
console.log(stringValue.slice(20));//''
console.log(stringValue.slice(2,undefined));//'llo world'

console.log(stringValue.slice(2,-5));//'llo '
console.log(stringValue.slice(2,-20));//''
console.log(stringValue.slice(-2,2));//''
console.log(stringValue.slice(-2,-20));//''      
console.log(stringValue.slice(-2,20));//'ld'
console.log(stringValue.slice(-20,2));//'he'
console.log(stringValue.slice(-20,-2));//'hello wor'

位置

字符串和數(shù)組都擁有查找位置的兩個(gè)方法:indexOf()和lastIndexOf()。位置方法和中括號(hào)[]讀取方法正好相反,一個(gè)是通過項(xiàng)查找索引,一個(gè)是通過索引查找項(xiàng)

【indexOf()】

indexOf(search,start)方法接收search和start兩個(gè)參數(shù),返回search首次出現(xiàn)的位置,如果沒有找到則返回-1

字符串中的search參數(shù)會(huì)調(diào)用String()轉(zhuǎn)型函數(shù),將該參數(shù)的非字符串值轉(zhuǎn)換為字符串;而數(shù)組中的search參數(shù)則使用嚴(yán)格相等運(yùn)算符(===)進(jìn)行比較

不論是數(shù)組還是字符串,第二個(gè)參數(shù)start都會(huì)隱式調(diào)用Number()轉(zhuǎn)型函數(shù),將start非數(shù)字值(undefined除外)轉(zhuǎn)換為數(shù)值;若忽略該參數(shù)或該參數(shù)為undefined、NaN時(shí),start = 0

若start參數(shù)為負(fù)數(shù),字符串的處理是將start=0;而數(shù)組的處理是start = max(0,start+length)

var string = 'hello world world';
console.log(string.indexOf('ld'));//9
console.log(string.indexOf('ld',undefined));//9
console.log(string.indexOf('ld',NaN));//9
console.log(string.indexOf('ld',-1));//9
console.log(string.indexOf('ld',10));//15
console.log(string.indexOf('ld',[10]));//15
console.log(string.indexOf('true',[10]));//-1
console.log(string.indexOf(false,[10]));//-1
var arr = ['a','b','c','d','e','a','b'];
console.log(arr.indexOf('a',undefined));//0
console.log(arr.indexOf('a',NaN));//0
console.log(arr.indexOf('a',1));//5
console.log(arr.indexOf('a',true));//5
console.log(arr.indexOf('a',-1));//max(0,-1+7)=6; -1
console.log(arr.indexOf('a',-5));//max(0,-5+7)=2; 5
console.log(arr.indexOf('a',-50));//max(0,-50+7)=0; 0

【lastIndexOf()】

與indexOf()方法相反,lastIndexOf()方法是從右向左查找

lastIndexOf(search,start)方法接收search和start兩個(gè)參數(shù),返回searchString第一次出現(xiàn)的位置,如果沒有找到則返回-1

類似地,字符串中的search參數(shù)會(huì)調(diào)用String()轉(zhuǎn)型函數(shù),將該參數(shù)的非字符串值轉(zhuǎn)換為字符串;而數(shù)組中的search參數(shù)則使用嚴(yán)格相等運(yùn)算符(===)進(jìn)行比較

不論是數(shù)組還是字符串,第二個(gè)參數(shù)start都會(huì)隱式調(diào)用Number()轉(zhuǎn)型函數(shù),將start非數(shù)字值(undefined除外)轉(zhuǎn)換為數(shù)值

若忽略該參數(shù)或該參數(shù)為undefined、NaN時(shí),字符串的處理是start = length - 1;而數(shù)組的處理是start = 0

若start參數(shù)為負(fù)數(shù),字符串的處理是將start=0;而數(shù)組的處理是start = max(0,start+length)

var string = 'hello world world';
console.log(string.lastIndexOf('ld'));//15
console.log(string.lastIndexOf('ld',undefined));//15
console.log(string.lastIndexOf('ld',NaN));//15
console.log(string.lastIndexOf('ld',-1));//-1
console.log(string.lastIndexOf('h',-1));//0
console.log(string.lastIndexOf('w',undefined));//12

console.log(string.lastIndexOf('ld',10));//9
console.log(string.lastIndexOf('ld',[10]));//9
console.log(string.lastIndexOf('true',[10]));//-1
console.log(string.lastIndexOf(false,[10]));//-1
var arr = [1,2,3,'1','2','3'];
console.log(arr.lastIndexOf('2'));//4
console.log(arr.lastIndexOf(3));//2
console.log(arr.lastIndexOf(0));//-1

var arr = ['a','b','c','d','e','a','b'];
console.log(arr.lastIndexOf('b'));//6
console.log(arr.lastIndexOf('b',undefined));//-1
console.log(arr.lastIndexOf('a',undefined));//0
console.log(arr.lastIndexOf('b',NaN));//-1
console.log(arr.lastIndexOf('b',1));//1
console.log(arr.lastIndexOf('b',-1));//max(0,-1+7)=6; 6
console.log(arr.lastIndexOf('b',-5));//max(0,-5+7)=2; 1
console.log(arr.lastIndexOf('b',-50));//max(0,-50+7)=0; -1

以上這篇javascript中數(shù)組和字符串的方法對(duì)比就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • 通過實(shí)例了解JS執(zhí)行上下文運(yùn)行原理

    通過實(shí)例了解JS執(zhí)行上下文運(yùn)行原理

    這篇文章主要介紹了通過實(shí)例了解JS執(zhí)行上下文運(yùn)行原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • JavaScript運(yùn)行過程中的“預(yù)編譯階段”和“執(zhí)行階段”

    JavaScript運(yùn)行過程中的“預(yù)編譯階段”和“執(zhí)行階段”

    這篇文章主要介紹了JavaScript運(yùn)行過程中的“預(yù)編譯階段”和“執(zhí)行階段”的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 淺談JS正則表達(dá)式的RegExp對(duì)象和括號(hào)的使用

    淺談JS正則表達(dá)式的RegExp對(duì)象和括號(hào)的使用

    下面小編就為大家?guī)硪黄獪\談JS正則表達(dá)式的RegExp對(duì)象和括號(hào)的使用。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • JavaScript中雙向數(shù)據(jù)綁定詳解

    JavaScript中雙向數(shù)據(jù)綁定詳解

    這篇文章主要為大家詳細(xì)介紹了JavaScript中雙向數(shù)據(jù)綁定,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-05-05
  • 使用taro開發(fā)微信小程序遇到的坑總結(jié)

    使用taro開發(fā)微信小程序遇到的坑總結(jié)

    Taro,京東凹凸實(shí)驗(yàn)室出品的適配多端的一個(gè)框架,這篇文章主要介紹了使用taro開發(fā)微信小程序遇到的坑總結(jié),需要的朋友可以參考下
    2019-04-04
  • JavaScript代碼執(zhí)行的先后順序問題

    JavaScript代碼執(zhí)行的先后順序問題

    今天就給大家介紹一個(gè)特別基礎(chǔ)的東西,javascript中函數(shù)的一點(diǎn)兒小知識(shí)之js代碼的執(zhí)行順序問題,需要的朋友參考下吧
    2017-10-10
  • JS使用Promise控制請(qǐng)求并發(fā)數(shù)

    JS使用Promise控制請(qǐng)求并發(fā)數(shù)

    現(xiàn)在面試過程當(dāng)中 ,手寫題必然是少不了的,其中碰到比較多的無非就是當(dāng)屬 請(qǐng)求并發(fā)控制了,所以本文為大家整理了JS使用Promise控制請(qǐng)求并發(fā)數(shù)的示例代碼,希望對(duì)大家有所幫助
    2023-05-05
  • JavaScript實(shí)現(xiàn)滑動(dòng)門效果

    JavaScript實(shí)現(xiàn)滑動(dòng)門效果

    這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)滑動(dòng)門效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-01-01
  • Rollup處理并打包JS文件項(xiàng)目實(shí)例代碼

    Rollup處理并打包JS文件項(xiàng)目實(shí)例代碼

    rollup是一款用來es6模塊打包代碼的構(gòu)建工具(支持css和js打包)。這篇文章主要介紹了Rollup處理并打包JS文件項(xiàng)目實(shí)例,需要的朋友可以參考下
    2018-05-05
  • 手把手帶你入門微信小程序新框架Kbone的使用

    手把手帶你入門微信小程序新框架Kbone的使用

    這篇文章主要介紹了手把手帶你入門微信小程序新框架Kbone的使用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-02-02

最新評(píng)論

丰都县| 亳州市| 长宁区| 蒲江县| 上高县| 福鼎市| 凌云县| 张北县| 通州市| 河源市| 武城县| 酉阳| 北安市| 花莲县| 赞皇县| 曲周县| 红河县| 饶河县| 阳谷县| 通辽市| 邛崃市| 铅山县| 山阳县| 谷城县| 汕头市| 丹巴县| 广汉市| 寿阳县| 固安县| 耿马| 噶尔县| 布拖县| 九寨沟县| 九江市| 文成县| 叶城县| 天等县| 丰镇市| 湖州市| 信丰县| 福清市|