建立數組的基本方式有兩種:正則表達式
使用Array構造函數數組
使用數組字面量表示法dom
數組的棧和隊列方法:函數
var colors = new Array(); var count = colors.push("red","green","orange"); console.log(count); //3 var item = colors.pop(); //取得最後一項 orange console.log(item); console.log(colors.length); //2 var colors = new Array(); var count = colors.unshift("red","green"); count = colors.unshift("black"); //添加到數組第一個元素 console.log(colors); var item = colors.shift(); //取得第一項 black console.log(item); console.log(colors.length); //2
var words = ["border","left"]; var wordstr = words.join(); console.log(wordstr); //結果爲:border,left join()默認是逗號隔開 var wordstr = words.join("-"); console.log(wordstr); //結果爲:border-left
var values = [1,2,3,4,5]; var newValues = values.reverse(); console.log(newValues);
var arr = [9,5,45]; arr.sort(function(a,b){return b-a}); //降序 arr.sort(function(a,b){return a-b}); //升序
var arr1 = ["a","b"], arr2 = ["c","d"], arr3; arr3 = arr1.concat(arr2); console.log(arr3);
var colors = ["red","green","blue"]; var newColors = colors.slice(1,2); console.log(newColors); //結果輸出 green var newColors = colors.slice(0); console.log(newColors); //結果輸出 ["red","green","blue"] var newColors = colors.slice(-1,3); //負數 + 數組長度 console.log(newColors); //結果輸出 blue
splice()方法刪除數組項、插入數組項、替換數組項編碼
var arr = ["a","b","c"]; var delArr = arr.splice(1,1); console.log(delArr); //返回 b
var arr = ["a","b","c"]; var insertArr = arr.splice(3,0,"m"); console.log(arr); //["a","b","c","m"]
var arr = ["a","b","c"]; var replaceArr = arr.splice(1,2,"x","y"); console.log(replaceArr); //["b","c"] console.log(arr); //["a","x","y"]
ECMAScript爲數組實例化添加的兩個位置方法:url
//功能:從數組的開頭(位置0)開始向後查找,沒有找到的狀況下返回-1 var nums = [1,2,3,4,5,6]; var pos = nums.indexOf(2); console.log(pos); //返回3的位置 2
var nums = [1,2,3,4,5,2,6]; var pos = nums.lastIndexOf(2); console.log(pos); //返回2的位置 5
字符方法:code
//訪問字符串中的特定字符 var str = "hello world"; console.log(str.charAt(0)); //返回自己字符 h console.log(str.charCodeAt(0)); //返回自己的字符編碼 104
字符串操做方法:orm
//用於將一或多個字符串拼接起來,返回拼接獲得的新字符串 //但實踐中使用更可能是加號操做符(+) var str = "hello "; var result = str.concat("world"); console.log(result); //輸出 hello world
//start:必需,指定子字符串的開始位置 end:可選,表示子字符串到哪裏結束 var str = "hello world"; console.log(str.slice(3)); //lo world console.log(str.substring(3)); //lo world console.log(str.substr(3)); //lo world console.log(str.slice(3,5)); //lo console.log(str.substring(3,5)); //lo console.log(str.substr(3,5)); //lo wo console.log(str.slice(-3)); //rld 負數 + 字符串長度 console.log(str.substring(-3)); //hello world 會將全部負值參數都轉換爲0 console.log(str.substr(-3)); //rld console.log(str.slice(3,-4)); //lo w console.log(str.substring(3,-4)); //hel console.log(str.substr(3,-4)); //""(空字符串) 參數一:負數+字符串長度 參數二:轉換爲0
字符串位置方法對象
//從一個字符串中搜索給定的子字符串,返回子字符串的位置 // 返回值:數值 //若是沒有找到該子字符串,則返回-1 var email= "marry@163.com"; console.log(email.indexOf("@")); //返回 5 var email= "marry.mail@163.com"; console.log(email.lastIndexOf(".")); //返回 14
//獲取擴展名 var url = "http://baidu.com/index.txt"; function getFileFormat(url){ var pos = url.lastIndexOf("."); return url.substr(pos); } var formatName = getFileFormat(url); console.log(formatName);
//刪除前置及後綴的全部空格 var str = " hello world "; var newStr = str.trim(); console.log(newStr); //hello world
var str = "hello world"; console.log(str.toUpperCase()); //大寫 console.log(str.toLowerCase()); //小寫
var str = "welcome-to-beijing"; var arr = str.split("-"); console.log(arr); //輸出 ["welcome", "to", "beijing"]
var str = "cat,bat,sat"; var result = str.replace("at","ond"); var newResult = str.replace("/at/g","ond"); //替換全部 console.log(result); //輸出 cond,bat,sat 只是替換第一個
var max = Math.max(1,2,3,4); console.log(max); //4 var min= Math.min(1,2,3,4); console.log(min); //1
console.log(Math.ceil(25.6)); //26,向上舍入 console.log(Math.round(25.5)); //26,四捨五入 console.log(Math.floor(25.6)); //25, 向下舍入
//值 = Math.floor(Math.random() * 可能值的總數 + 第一個可能的值) var num = Math.floor(Math.random() * 10 + 1); console.log(num); //輸出1-10之間的數值 var num = Math.floor(Math.random() * 9 + 2); console.log(num); //輸出2-10之間的數值
var now = new Date();
var now = new Date(); console.log(now.toDateString()); //Wed May 01 2019 console.log(now.toTimeString()); //11:26:38 GMT+0800 (中國標準時間) console.log(now.toLocaleDateString()); //2019/5/1 console.log(now.toLocaleTimeString()); //上午11:26:38 console.log(now.toUTCString()); //Wed, 01 May 2019 03:26:38 GMT
set/getFullYear 設置/返回4位數的年份 set/getMonth() 設置/返回月份(0表示一月,11表示十二月) set/getDate() 設置/返回天數(1到31) set/getDay() 設置/返回星期幾(0表示星期日,6表示星期六) set/getHours() 設置/返回小時(0到23) set/getMinutes 設置/返回分鐘數(0到59) set/getSeconds 設置/返回秒數(0到59)
第一次嘗試着寫,寫得很差的地方,請多多指教