天天看點

第199天:js---擴充内置對象功能總結

一、數組

1、删除數組中指定索引的資料

1 /** 删除數組中指定索引的資料 **/
2 Array.prototype.deleteAt = function (index) {
3     if (index < 0) {
4         return this;
5     }
6     return this.slice(0, index).concat(this.slice(index + 1, this.length));
7 }      

2、數組洗牌

1 /** 數組洗牌 **/
 2 Array.prototype.random = function () {
 3     var tempArr = [], me = this, t;
 4     while (me.length > 0) {
 5         t = Math.floor(Math.random() * me.length);
 6         tempArr[tempArr.length] = me[t];
 7         me = me.deleteAt(t);
 8     }
 9     return tempArr;
10 }
11 Array.prototype.orderRandom = function () {
12     return this.sort(function () {
13         return Math.random() > 0.5 ? "-1" : "1";
14     });
15 }      

3、數字數組排序

1 /** 數字數組排序 **/
 2 Array.prototype.sortNum = function (i) {
 3     if (!i) {
 4         i = 0;
 5     }
 6     if (i == 1) {
 7         return this.sort(function (a, b) {
 8             return b - a;
 9         });
10     }
11     return this.sort(function (a, b) {
12         return a - b;
13     });
14 }      

4、擷取數字數組中的最大項

1 /** 擷取數字數組中的最大項 **/
2 Array.prototype.getMax = function () {
3     return this.sortNum(1)[0];
4 }      

5、擷取數字數組中的最小項

1 /** 擷取數字數組中的最小項 **/
2 Array.prototype.getMin = function () {
3     return this.sortNum(0)[0];
4 }      

6、數組第一次出現指定元素的位置

1 /** 數組第一次出現指定元素的位置 **/
2 Array.prototype.indexOf = function (o) {
3     for (var i = 0; i < this.length; i++) {
4         if (this[i] == o) {
5             return i;
6         }
7     }
8     return -1;
9 }      

7、去除數組中的重複項

1 /** 去除數組中的重複項 **/
 2 Array.prototype.arrUnique = function () {
 3     var reset = [], done = {};
 4     for (var i = 0; i < this.length; i++) {
 5         var temp = this[i];
 6         if (!done[temp]) {
 7             done[temp] = true;
 8             reset.push(temp);
 9         }
10     }
11     return reset;
12 }      

二、常用string内置對象方法

1、concat() – 将兩個或多個字元的文本組合起來,傳回一個新的字元串

1 //concat() – 将兩個或多個字元的文本組合起來,傳回一個新的字元串。
2     var str = "Hello";
3     var out = str.concat(" World","!");
4     console.log(str); //Hello
5     console.log(out); //Hello World!       

2、charAt() – 傳回指定位置的字元

1 //charAt() – 傳回指定位置的字元。 
2     var str = "HelloString";
3     var out = str.charAt(1);
4     console.log(out); //e      

3、charCodeAt() – 傳回在指定的位置的字元的 Unicode 編碼

1 //charCodeAt() – 傳回在指定的位置的字元的 Unicode 編碼。
2     var str = "HelloString";
3     var out = str.charCodeAt(5);
4     console.log(out); //83      

4、indexOf(searchvalue,fromindex) – 傳回字元串中一個子串第一處出現的索引,如果沒有比對項,傳回 -1 

1 //indexOf(searchvalue,fromindex) – 傳回字元串中一個子串第一處出現的索引,如果沒有比對項,傳回 -1 。 
2     //fromindex是可選的整數參數。規定在字元串中開始檢索的位置。它的合法取值是 0 到 stringObject.length - 1。
3     //如省略該參數,則将從字元串的首字元開始檢索,此時indexOf()跟search()方法類似。
4     var str = "HelloStringend";
5     console.log(str.indexOf("e")); //1
6     console.log(str.indexOf("e",2)); //11      

5、lastIndexOf(searchvalue,fromindex) – 傳回字元串中一個子串最後一處出現的索引,如果沒有比對項,傳回 -1

1 //lastIndexOf(searchvalue,fromindex) – 傳回字元串中一個子串最後一處出現的索引,如果沒有比對項,傳回 -1 。 
2     //如果在 stringObject 中的 fromindex 位置之前存在 searchvalue,則傳回的是出現的最後一個 searchvalue 的位置。
3     var str = "HelloString";
4     console.log(str.lastIndexOf("l")); //3
5     console.log(str.lastIndexOf("l", 1)); //-1
6     console.log(str.lastIndexOf("l", 2)); //2
7     console.log(str.lastIndexOf("l", 3)); //3      

6、substring(start,end) – 傳回一個新的字元串,包括 start 處的字元,但不包括 end 處的字元,其長度為 end 減 start

1 //substring(start,end) – 傳回一個新的字元串,包括 start 處的字元,但不包括 end 處的字元,其長度為 end 減 start
 2     //substring是以兩個參數中較小一個作為起始位置,較大的參數作為結束位置。
 3     //注意:如果參數是負數,substring則會将負參數都直接轉換為0,如果僅有一個參數,則從start開始到末尾。 
 4     var str = "HelloExample";
 5     console.log(str); //HelloExample
 6     console.log(str.substring(1, 3)); //el
 7     console.log(str.substring(3, 1)); //el
 8     console.log(str.substring(2)); //lloExample
 9     console.log(str.substring(-1)); //HelloExample
10     console.log(str.substring(-1, -3)); //空字元串
11     console.log(str.substring(-1, 5)); //Hello      

7、substr(start [,length]) – 傳回一個新的字元串,從起始索引号提取字元串中指定數目的字元

1 //substr(start [,length]) – 傳回一個新的字元串,從起始索引号提取字元串中指定數目的字元。
 2     //如果僅有一個參數,則從start開始到末尾。
 3     //當接收的參數是負數時,substr是将第一個參數與字元串長度相加後的結果作為第一個參數
 4     var str = "HelloExample";
 5     console.log(str); //HelloExample
 6     console.log(str.substr(1, 3)); //ell
 7     console.log(str.substr(2, -1)); //空字元串
 8     console.log(str.substr(1)); //elloExample
 9     console.log(str.substr(-4, 2)); //mp
10     console.log(str.substr(-3)); //ple      

8、stringObject.slice(start,end) 傳回一個新的字元串,包括 start 處的字元,但不包括 end 處的字元,其長度為 end 減 start

1 //stringObject.slice(start,end) 傳回一個新的字元串,包括 start 處的字元,但不包括 end 處的字元,其長度為 end 減 start
 2     //當接收的參數是負數時,slice會将它字元串的長度與對應的負數相加,結果作為參數。如果僅有一個參數,則從start開始到末尾。
 3     var str = "HelloExample";
 4     console.log(str); //HelloExample
 5     console.log(str.slice(1, 3)); //el
 6     console.log(str.slice(2)); //lloExmaple
 7     console.log(str.slice(3, 1)); //空字元串
 8     console.log(str.slice(-4, -1)); //mpl
 9     console.log(str.slice(-4, 0)); //空字元串
10     console.log(str.slice(-1, -4)); //空字元串
11     console.log(str.slice(1, -4)); //elloExa      

9、将字元串轉換成大/小寫字母

1 //toLowerCase() – 将整個字元串轉成小寫字母。
2     //toUpperCase() – 将整個字元串轉成大寫字母。
3     var str = "How Are you";
4     console.log(str.toLowerCase()); //how are you
5     console.log(str.toUpperCase()); //HOW ARE YOU      

10、支援正規表達式的 String 對象的方法

1 /*split,match,replace,search*/
 2     //stringObject.split(separator,howmany),傳回一個字元串數組。該數組是通過在 separator 指定的邊界處将字元串 stringObject 分割成子串建立的。傳回的數組中的字串不包括 separator 自身。
 3     var str = "How are you doing today";
 4     console.log(str.split(" ")); //["How", "are", "you", "doing", "today"]
 5     console.log(str); //How are you doing today
 6     console.log(str.split("")); //["H", "o", "w", " ", "a", "r", "e", " ", "y", "o", "u", " ", "d", "o", "i", "n", "g", " ", "t", "o", "d", "a", "y"] 
 7     console.log(str.split("", 3)); //["H", "o", "w"]
 8     console.log(str.split("a")); //["How ", "re you doing tod", "y"]
 9     console.log(str.split("good")); //["How are you doing today"]
10     var str = "a_db-c(d+e";
11     console.log(str.split(/[^a-z]/i)); //["a", "db", "c", "d", "e"]       

(1)match(searchvalue) 

1 /** match(searchvalue) 或 match(regexp)檢查一個字元串是否比對一個正規表達式。傳回存放比對結果的數組。
 2     match() 方法将檢索字元串 stringObject,以找到一個或多個與 regexp 比對的文本。這個方法的行為在很大程度上有賴于 regexp 是否具有标志 g。
 3     如果 regexp 沒有标志 g,那麼 match() 方法就隻能在 stringObject 中執行一次比對。如果沒有找到任何比對的文本, match() 将傳回 null。否則,它将傳回一個數組,其中存放了與它找到的比對文本有關的資訊。該數組的第 0 個元素存放的是比對文本,而其餘的元素存放的是與正規表達式的子表達式比對的文本。除了這些正常的數組元素之外,傳回的數組還含有兩個對象屬性。index 屬性聲明的是比對文本的起始字元在 stringObject 中的位置,input 屬性聲明的是對 stringObject 的引用。
 4     如果 regexp 具有标志 g,則 match() 方法将執行全局檢索,找到 stringObject 中的所有比對子字元串。若沒有找到任何比對的子串,則傳回 null。如果找到了一個或多個比對子串,則傳回一個數組。不過全局比對傳回的數組的内容與前者大不相同,它的數組元素中存放的是 stringObject 中所有的比對子串,而且也沒有 index 屬性或 input 屬性。
 5     **/
 6     var str = "Hello world! Hello";
 7     console.log(str.match("lo")); //["lo"] { index: 3, input: "Hello world! Hello" }
 8     console.log(str.match("world")); //["world"] { index: 6, input: "Hello world! Hello" }
 9     console.log(str.match("world").index); //6
10     console.log(str.match(/Hello/g)); //["Hello", "Hello"]
11     console.log(str.match("World")); //null
12     var str = "1 plus 2 equal 3";
13     console.log(str.match(/\d+/g)); //["1", "2", "3"]      

(2)replace() 

1  //replace() 方法用于在字元串中用一些字元替換另一些字元,或替換一個與正規表達式比對的子串。
 2     //stringObject.replace(regexp/substr,replacement),傳回一個新的字元串,是用 replacement 替換了 regexp 的第一次比對或所有比對之後得到的。
 3     //replacement 可以是字元串,也可以是函數。如果它是字元串,那麼每個比對都将由字元串替換。但是 replacement 中的 $ 字元具有特定的含義,它說明從模式比對得到的字元串将用于替換。
 4     //  $1、$2、...、$99----與 regexp 中的第 1 到第 99 個子表達式相比對的文本。
 5     //  $&------------------與 regexp 相比對的子串。
 6     //  $`------------------位于比對子串左側的文本。
 7     //  $'------------------位于比對子串右側的文本。
 8     //  $$------------------直接量符号。
 9     var str = "Visit HangZhou";
10     console.log(str); //Visit HangZhou
11     console.log(str.replace(/Hang/g, "Su")); //Visit SuZhou
12     var str = "1 plus 2 equal 3";
13     console.log(str); //1 plus 2 equal 3
14     console.log(str.replace(/(\d)/g, "*"));  //* plus * equal *
15 
16     var str = "as An angle";
17     console.log(str.replace(/a/, "b")); //bs An angle
18     console.log(str.replace(/a/g, "b")); //bs An bngle
19     console.log(str.replace(/a/gi, "b")); //bs bn angle
20 
21     var str = "Karl,John";
22     console.log(str.replace(/(\w+)\s*,\s*(\w+)/, "$2,$1")); //John,Karl
23 
24     var str = '"ab", "b"';
25     console.log(str.replace(/"([^"]*)"/g, "'$1'")); //'ab', 'b' 
26 
27     var str = "aaa bbb ccc";
28     console.log(str.replace(/\b\w+\b/g, function (w) {
29         return w.substring(0, 1).toUpperCase() + w.substring(1);
30     })); //Aaa Bbb Ccc       

(3)search() – 執行一個正規表達式比對查找

1 //search() – 執行一個正規表達式比對查找。如果查找成功,傳回字元串中比對的索引值。否則傳回 -1 。 
 2     //stringObject.search(regexp) 傳回stringObject 中第一個與 regexp 相比對的子串的起始位置。
 3     //此方法跟indexOf類似。此方法會忽略g辨別
 4     var str = "Visit HangZhou";
 5     console.log(str) //Visit HangZhou
 6     console.log(str.search(/Hang/)); //6
 7     console.log(str.search(/hang/)); //-1
 8     console.log(str.search(/hang/i)); //6
 9     var str = "1 plus 2 equal 3";
10     console.log(str) //1 plus 2 equal 3
11     console.log(str.search(/\d/g)); //0      

繼續閱讀