回顧一下基礎(chǔ)知識。有些方法時間長不用都忘了...
字符串的常用方法
var str = 'abc';
- charAt 返回指定索引處的字符
console.log(str.charAt(1));
//'b'
- charCodeAt 返回指定索引處的unicode字符
console.log(str.charCodeAt(1));
//98
- indexof 判斷傳入的字符第一次出現(xiàn)在字符串當(dāng)值出現(xiàn)的位置,如果包含返回它的索引,反之返回-1
console.log(str.indexOf('b'));
//1
- concat 拼接兩個字符串,返回一個新字符串
var str1='123';
var str2=str.concat(str1);
console.log(str2);
//"123abc"
- substr(n,m) 從n開始截取m個字符,將截取的字符返回,對原字符串沒有任何改變。
console.log(str.substr(1,2));
//'bc'
- substring(n,m) 從索引n開始,截取到索引m,不包括m.將截取的字符返回,對原字符串沒有任何改變.
console.log(str.substring(1,2));
//'b'
- slice(n,m) 從索引n開始截取到m, 如果只有一個參數(shù)截取到字符串末尾,原字符串保持不變。
console.log(str.slice(1,2));
//'b'
console.log(str.slice(1));
//'bc'
- split 用指定字符分割字符串,返回一個數(shù)組,原字符串保持不變
console.log(str.split(''));
//[ 'a', 'b', 'c' ]
- replace(n,m) 使用m字符替換指定字符n,一般搭配正則使用,改變原字符串
console.log(str.replace(reg,1));
//'1bc'
- match() 在字符串內(nèi)檢測指定的值,找到一個或多個正則表達(dá)式的匹配,把找到的字符串放在一個數(shù)組里,返回數(shù)組。
console.log(str.match(reg));
//['a']
- includes('a') 傳入指定字符,返回布爾值,匹配到返回true 第二個參數(shù)表示開始搜索的位置
console.log(str.includes('a'));
//true
- startsWith() 傳入字符是否存在原字符串的開始位置,匹配到返回true 第二個參數(shù)表示開始搜索的位置
console.log(str.startsWith('b'));
//false
console.log(str.startsWith('b',1));
//true
- endsWith() 傳入字符是否存在原字符串的末尾位置,匹配到返回true 第二個參數(shù)表示開始搜索的位置
console.log(str.endsWith('c'));
//true
console.log(str.endsWith('b',2));
//true
- repeat() 方法返回一個新字符串,表示將原字符串重復(fù)n次。
console.log(str.repeat(2));
//'abcabc'
- padStart(),padEnd()字符串補全長度的功能, padStart()用于頭部補全,padEnd()用于尾部補全。
第一個參數(shù)表示字符串最小長度,第二個參數(shù)是用來補全的字符串。
console.log(':12'.padStart(5,'00'));
//'00:12'
console.log(':12'.padEnd(5,'00'));
//':1200'
如果原字符串加上補齊后的字符串長度大于或等于參數(shù)設(shè)置的最小長度,竊取超出的部分,返回原字符串長度
console.log('1234'.padEnd(5,'00'));
//'12340'
數(shù)組的常用方法
var arr =[1,2,3,4];
轉(zhuǎn)化方法
- toString() toLocaleString()
將數(shù)組轉(zhuǎn)換成字符串,以逗號分隔
console.log(arr.toLocaleString());
console.log(arr.toString());
//'1,2,3,4,2,4,1'
- join()方法 通過指定分割符分割返回一個字符串
console.log(arr.join('-'));
//'1-2-3-4'
- pop 刪除數(shù)組最后一項,返回刪除那一項,改變原數(shù)組
- push 向數(shù)組末尾添加一項,返回添加那一項,改變原數(shù)組
console.log(arr.push(5));
//[ 1, 2, 3, 4, 5 ]
console.log(arr.pop(5));
//[ 1, 2, 3, 4]
- shift() 刪除數(shù)組第一項,返回刪除這一項,改變原數(shù)組
- unshift() 如果不傳參數(shù),返回數(shù)組的長度,不改變數(shù)組,如果傳遞參數(shù),向數(shù)組第一項添加指定參數(shù),改變數(shù)組,返回新數(shù)組。
console.log(arr.shift(3))
//[ 2, 3, 4 ]
console.log(arr.unshift(11,22),arr);
//6 [ 11, 22, 1, 2, 3, 4 ]
排序方法
- reverse() sort() 正序和倒序排列數(shù)組,但是會存在問題因為
如果省略參數(shù),數(shù)組項會先根據(jù)toString()函數(shù)將其值轉(zhuǎn)換成字符串在進(jìn)行比較,是按UNICODE進(jìn)行比較的。所以一般情況下,使用下面代碼進(jìn)行比較。
//正序
console.log(arr.sort(function (a,b) {
return a-b
}),arr);
//[ 1, 2, 3, 34, 100, 415 ]
//倒敘
console.log(arr.sort(function (a,b) {
return b-a
}),arr);
//[ 415, 100, 34, 3, 2, 1 ]
操作方法
- concat() 連接兩個數(shù)組,返回新數(shù)組,不變異原數(shù)組
- slice(satrt,end) start必須從指定索引位置開始讀取到結(jié)尾,如果是負(fù)數(shù),從尾部開始位置-1是最后一個,-2為倒數(shù)第一個加上倒數(shù)第二個,以此類推。并且原數(shù)組不變異。
end 可選,從指定索引位置結(jié)束讀取。并且不保含指定位置的元素
console.log(arr.slice(2),arr);
//[ 3, 4 ] [ 1, 2, 3, 4 ]
console.log(arr.slice(1,3),arr);
[ 2, 3 ] [ 1, 2, 3, 4 ]
-
splice() 方法用于插入、刪除或替換數(shù)組的元素 變異原數(shù)組
- 刪除 傳遞兩個參數(shù)(start,end) 開始元素的索引和結(jié)束元素的索引,包含結(jié)束位置,返回刪除的數(shù)組
console.log(arr.splice(1,2),arr); //[ 2, 3 ] [ 1, 4 ]- 插入 三個參數(shù) 第一個參數(shù)開始的索引,第二個參數(shù)要刪除幾項,如果為0,就是不刪除,添加。第三個參數(shù)要添加的元素。返回刪除那一項
console.log(arr.splice(1,0,23),arr); //[] [ 1, 23, 2, 3, 4 ] console.log(arr.splice(1,2,23),arr); // [ 2, 3 ] [ 1, 23, 4 ]- 替換
console.log(arr.splice(1,1,22),arr); //[ 2 ] [ 1, 23, 3, 4 ]
遍歷方法
- every
- some()
- fifter()
- reduce()
- map()
- includes()
- find()
- 以上方法對原數(shù)組都不變異
map 映射(將原有數(shù)組映射成新數(shù)組)
let arr2 = arr.map(function(item){
return item *= 3
})
//[3,6,9,10]
filter() 返回為true的這一項,并把它放入新數(shù)組中。
var arr = [1,2,3,4,5]
let newArr = arr.filter(function(item){
return item<5&&item >2
})
console.log(newArr)
// 3,4
every() 如果一個條件為false,停止遍歷,返回false
some() 如果一個條件為true,停止遍歷,返回true
includes 查找數(shù)組中有沒有某值
let arr3 = [1,2,3,4,55];
console.log(arr3.includes(5));
//false
返回false 說明這個方法無法匹配多位的,我們還有一個方法可以解決這個方法的不足
find() 返回為true的這一項,并把它放入新數(shù)組中
let result = arr3.find(function(item){
return item.toString().indexOf(5) >-1
})
console.log(result)
//55