Array String對象的方法和屬性

Array


注意:如下例子都是在一層層進行操做的(保留上一步的操做)。正則表達式

示例:var arr = [1,2,3,4,5,6];數組

1.arr.length:獲取數組元素的長度

console.log(arr.length); // 6

2.arr.join(str):將arr以指定字符鏈接成字符串

var str = ':';
console.log(arr.join(str)); // 1:2:3:4:5:6

3.arr.push():在數組末尾推入指定元素

var str = 7;
console.log(arr.push(str)); // 7
console.log(arr); // [1,2,3,4,5,6,7]

4.arr.pop():彈出並返回數組末尾

console.log(arr.pop()); // 7

5.arr.shift():彈出並返回數組第一個元素

console.log(arr.shift()); // 1

6.arr.unshift():在數組開頭處添加指定元素

var str = 0;
arr.unshift(str);
console.log(arr); // [0,2,3,4,5,6] 第4,5步已經彈出了7和1。

7.arr.sort([函數:排序規則]):排序(默認採用字符串順序排序,數字排序則須要經過自定義函數實現)

console.log(arr.sort()); //按照字符串規則排序 // [0,2,3,4,5,6]
console.log(arr.sort(function(a,b){
    return a - b;
})); //按照數字順序排序 // [0,2,3,4,5,6]

8.arr.reverse():數組元素順序翻轉

console.log(arr.reverse()); // [6,5,4,3,2,0]

9.arr.indexOf():獲取指定元素在數組中的位置,不存在返回-1

console.log(arr.indexOf(6)); // 0 若是返回-1,說明數組裏沒有你指定的元素

10.arr.lastIndexOf():獲取指定元素最後一次出現的位置,不存在返回-1

console.log(arr.lastIndexOf(0)); // 5

11.arr.slice(起始位置,結束位置):獲取數組中指定的片斷(不包含結束位置)

console.log(arr.slice(2,3)); // [4]
console.log(arr); // [6,5,4,3,2,0]

12.arr.splice(起始位置,刪除個數,新增元素):從數組中添加或刪除元素

/*var sky = ['藍天','白雲','陽光','飛機'];
console.log(sky.length); // 4
var ress = sky.splice(1,0,'月亮');
console.log(sky); // ['藍天','月亮','白雲','陽光','飛機']*/

console.log(arr.splice(0,3)); // [6,5,4] 
console.log(arr); // [3,2,0]
var res = arr.splice(0,1,3,9);
console.log(arr); // [3,9,2,0]

示例:var arra = [12,24,35,3,78];函數

13.arra.every():檢測數值元素的每一個元素是否都符合條件

var res = arra.every(function(a){
    return a > 2;
});
console.log(res); // true

14.arra.map():經過指定函數處理數組的每一個元素,並返回處理後的數組

var res = arra.map(function(a){
    return a + 5;
});
console.log(res); // [17,29,40,8,83]

15.arra.filter():檢測數值元素,並返回符合條件全部元素的數組

var res = arra.filter(function(a){
    return a > 70; // 78
});
console.log(res);

16.arra.some():檢測數組元素中是否有元素符合指定條件

var res = arra.some(function(a){
    return a > 70; // true
});
console.log(res);

String


示例: var str1 = '就在這裏,不見,不散';編碼

1.str.length:字符串的長度

console.log(str1.length); // 10

2.str.split(str):將字符串以指定字符切割

var res = str1.split(',');
console.log(res); // 如輸入原字符串沒有的字符,則無變化
// ["就在這裏","不見","不散"]

3.str.search(str|reg):在字符串中搜索指定字符,返回對應的位置,不存在返回-1 檢索與正則表達式相匹配的值

console.log(str1.search(/不散/)); // 8 若是是英文字母要忽略大小寫,要加上i

示例:var str2 = '1 hello 2 world!';code

4.str.match(str|reg):在字符串中匹配指定字符,存在返回數組,不存在返回null 找到一個或多個正則表達式的匹配。g爲全局匹配

console.log(str2.match("hello")); // index:2
console.log(str2.match(/\d/g)); // ["1","2"]

5.str.replace(str1|reg,str2):用str2替換str1的值

console.log(str2.replace(/hello/,'good')); // 1 good 2 world!
console.log(str2); // 1 hello 2 world!

6.str.slice(start,end):獲取字符串中指定的片斷(不包含結束位置)

console.log(str2.slice(2,7)); // hello
console.log(str2); // 1 hello 2 world!

7.str.indexOf(str):獲取字符串中指定字符的位置,不存在返回-1

console.log(str2.indexOf('world')); // 10

8.str.lastIndexOf(str):獲取字符串中指定字符最後出現的位置,不存在返回-1

console.log(str2.lastIndexOf('o')); // 11

9.str.charAt(num):獲取指定位置的字符

console.log(str2.charAt(3)); // e

10.str.charCodeAt(num):指定位置的字母對應的Unicode編碼

console.log(str2.charCodeAt('3')); // 101

11.String.fromCharCode():將Unicode編碼轉爲字符

console.log(String.fromCharCode(65,66,67)); // ABC
相關文章
相關標籤/搜索