Array String对象的方法和属性
生活随笔
收集整理的這篇文章主要介紹了
Array String对象的方法和属性
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Array
注意:以下例子都是在一層層進行操作的(保留上一步的操作)。
示例:var arr = [1,2,3,4,5,6];
1.arr.length:獲取數組元素的長度
console.log(arr.length); // 62.arr.join(str):將arr以指定字符連接成字符串
var str = ':'; console.log(arr.join(str)); // 1:2:3:4:5:63.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()); // 75.arr.shift():彈出并返回數組第一個元素
console.log(arr.shift()); // 16.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)); // 511.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); // true14.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); // 102.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!';
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')); // 108.str.lastIndexOf(str):獲取字符串中指定字符最后出現的位置,不存在返回-1
console.log(str2.lastIndexOf('o')); // 119.str.charAt(num):獲取指定位置的字符
console.log(str2.charAt(3)); // e10.str.charCodeAt(num):指定位置的字母對應的Unicode編碼
console.log(str2.charCodeAt('3')); // 10111.String.fromCharCode():將Unicode編碼轉為字符
console.log(String.fromCharCode(65,66,67)); // ABC總結
以上是生活随笔為你收集整理的Array String对象的方法和属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle database data
- 下一篇: 第一天课程:第一个python程序pri