標籤:分割 sea object url aaa ora strong 屬性 sum
就是調用對象是一個Array,對Array類型增加了一個原型寫法的函數,一般寫一些擴充時經常用。比如判斷一個元素是否在數組中之類的
Array.prototype.inArray=function(value){ for(var i=0;i<this.length;i++){ if (this[i] == value){ return true; } } return false};var arr=["1","2","3"];//以下同調用方式,在inArray函數中,使用this即可得到arrarr.inArray("1");
js數組去重的三種常用方法總結
第一種是比較常規的方法
思路:
1.構建一個新的數組存放結果
2.for迴圈中每次從原數組中取出一個元素,用這個元素迴圈與結果數組對比
3.若結果數組中沒有該元素,則存到結果數組中
複製代碼代碼如下:[this[0]];如果是數組的話它是指this的第一個元素。
Array.prototype.unique1 = function(){
var res = [this[0]];
for(var i = 1; i < this.length; i++){
var repeat = false;
for(var j = 0; j < res.length; j++){
if(this[i] == res[j]){
repeat = true;
break;
}
}
if(!repeat){
res.push(this[i]);
}
}
return res;
}
var arr = [1, ‘a‘, ‘a‘, ‘b‘, ‘d‘, ‘e‘, ‘e‘, 1, 0]
alert(arr.unique1());
console:
var arr = [1, ‘a‘, ‘a‘, ‘b‘, ‘d‘, ‘e‘, ‘e‘, 1, 0]
undefined
var res = [arr[0]];
undefined
res
[1]
var pattern = /aaaa/,
str = ‘aaaa‘;
console.log(pattern.test(str));
VM1599:3 true
undefined
第二種方法比上面的方法效率要高
思路:
1.先將原數組進行排序
2.檢查原數組中的第i個元素 與 結果數組中的最後一個元素是否相同,因為已經排序,所以重複元素會在相鄰位置
3.如果不相同,則將該元素存入結果數組中
複製代碼代碼如下:
Array.prototype.unique2 = function(){
this.sort(); //先排序
var res = [this[0]];
for(var i = 1; i < this.length; i++){
if(this[i] !== res[res.length - 1]){
res.push(this[i]);
}
}
return res;
}
var arr = [1, ‘a‘, ‘a‘, ‘b‘, ‘d‘, ‘e‘, ‘e‘, 1, 0]
alert(arr.unique2());
第二種方法也會有一定的局限性,因為在去重前進行了排序,所以最後返回的去重結果也是排序後的。如果要求不改變數組的順序去重,那這種方法便不可取了。
第三種方法(推薦使用)
思路:
1.建立一個新的數組存放結果
2.建立一個Null 物件
3.for迴圈時,每次取出一個元素與對象進行對比,如果這個元素不重複,則把它存放到結果數組中,同時把這個元素的內容作為對象的一個屬性,並賦值為1,存入到第2步建立的對象中。
說明:至於如何對比,就是每次從原數組中取出一個元素,然後到對象中去訪問這個屬性,如果能訪問到值,則說明重複。
複製代碼代碼如下:
Array.prototype.unique3 = function(){
var res = [];
var json = {};
for(var i = 0; i < this.length; i++){
if(!json[this[i]]){
res.push(this[i]);
json[this[i]] = 1;
}
}
return res;
}
var arr = [112,112,34,‘你好‘,112,112,34,‘你好‘,‘str‘,‘str1‘];
alert(arr.unique3());
JS使用Regex除去字串中重複字元的方法<script language="javascript"> str = "Google" str1 = str.replace(/(.).*\1/g,"$1") document.write(str + "<br>"); document.write(str1);</script>
-
prototype(JS對象)
在JavaScript中,prototype對象是實現物件導向的一個重要機制。每個函數就是一個對象(Function),函數對象都有一個子物件 prototype對象,類是以函數的形式來定義的。prototype表示該函數的原型,也表示一個類的成員的集合。在通過new建立一個類的執行個體對象的時候,prototype對象的成員都成為執行個體化對象的成員。1、該對象被類所引用,只有函數對象才可引用;2、在new執行個體化後,其成員被執行個體化,執行個體對象方可調用。同時,函數是一個對象,函數對象若直接聲明成員,不用被執行個體化即可調用。JS中的prototype:http://www.cnblogs.com/yjf512/archive/2011/06/03/2071914.html 最初對js中 object.constructor 的認識:http://www.cnblogs.com/qiantuwuliang/archive/2011/01/08/1930548.html js 內建的 map() 方法
http://www.cnblogs.com/lengyuehuahun/p/5642807.html
http://www.cnblogs.com/rocky-fang/p/5756733.html
JSRegex
JavaScript RegExp 對象RegExp 對象
RegExp 對象表示Regex,它是對字串執行模式比對的強大工具。
RegExp 對象屬性
| 屬性 |
描述 |
FF |
IE |
| global |
RegExp 對象是否具有標誌 g。 |
1 |
4 |
| ignoreCase |
RegExp 對象是否具有標誌 i。 |
1 |
4 |
| lastIndex |
一個整數,標示開始下一次匹配的字元位置。 |
1 |
4 |
| multiline |
RegExp 對象是否具有標誌 m。 |
1 |
4 |
| source |
Regex的源文本。 |
1 |
4 |
RegExp 對象方法
| 方法 |
描述 |
FF |
IE |
| compile |
編譯Regex。 |
1 |
4 |
| exec |
檢索字串中指定的值。返回找到的值,並確定其位置。 |
1 |
4 |
| test |
檢索字串中指定的值。返回 true 或 false。 |
1 |
4 |
支援Regex的 String 對象的方法
| 方法 |
描述 |
FF |
IE |
| search |
檢索與Regex相匹配的值。 |
1 |
4 |
| match |
找到一個或多個Regex的匹配。 |
1 |
4 |
| replace |
替換與Regex匹配的子串。 |
1 |
4 |
| split |
把字串分割為字串數組。 |
1 |
4 |
JSRegex基本用法(經典全)
http://www.jb51.net/article/72346.htm
js學習筆記1