【前端学习笔记02】JavaScript字符串、数组的一些操作方法
字符串操作:
//str.length "micromajor".length // 10 //str.charAt() "micromajor".charAt(0) // "m" "micromajor".charAt(100) // "" //str.indexOf() "micro-major".indexOf("-") // 5 "micro-major-web".indexOf("-") // 5 "micro-major".indexOf("major") // 6 "micromajor".indexOf("-") // -1 //str.search() "micromajor163".search(/[0-9]/) // 10 "micromajor163".search(/[A-Z]/) // -1 //str.match "micromajor163".match(/[0-9]/) // ["1"] "micromajor163".match(/[0-9]/g)// ["1","6","3"] "micromajor163".match(/[A-Z]/) // null //str.replace(' ',' '); "micromajor163".replace("163","###") // "micromajor###" "micromajor163".replace(/[0-9]/,"#") // "micromajor#63" "micromajor163".replace(/[0-9]/g,"#") // "micromajor###" "micromajor163".replace(/[0-9]/g,"") // "micromajor" //str.substring "micromajor".substring(5,7) // "ma" "micromajor".substring(5) // "major" //str.slice() 可传入负数 "micromajor".slice(5,7) // "ma" "micromajor".slice(5) // "major" "micromajor".slice(1,-1) // "icromajo" "micromajor".slice(-3) // "jor" //str.substr() "micromajor".substr(5,2) // "ma" "micromajor".substr(5) // "major" //str.split(); "micro major".split(" ") // ["micro","major"] "micro major".split(" ",1) // ["micro"] "micro2major".split(/[0-9]/) // ["micro","major"] //str.toLowerCase(); "MicroMajor".toLowerCase() // "micromajor" //str.toUpperCase(); "MicroMajor".toUpperCase() // "MICROMAJOR" //字符串拼接 "0571" + "-" + "88888888" // "0571-88888888" //转字符 String(163) // "163" String(null) // "null" // \转义 "micro\"major" // "micro"major" "micro\\major" // "micro\major" "micro\tmajor" // "micro major"
一些正则相关:
var str = 'aaa111bb22c3'; var rex = /[a-z]+([0-9]{3})/; //字符串匹配正则表达式 str.match(rex); //rex最后不带g,只获取第一个匹配的,且会把字符串中匹配正则表达式中括号内的部分也放到数组中。 // -->['aaa111', '111']; //带g -->['aaa111']; //测试字符串能否匹配表达式 rex.test(str);// --> true //替换 str.replace(rex,'$1');// --> '111bb22c3'
数组操作:
//arr.indexOf(); var telephones = [110,120,114]; telephones.indexOf(120); // 1 telephones.indexOf(119); // -1 //arr.forEach(function(item,index,array)); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; var editScore = function(item,index,array){ item.score += 5; }; students.forEach(editScore); //arr.reverse(); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.reverse(); students[0].score; // 70 //arr.sort(function(a,b)); // function(a,b)会返回一个值,如果是小于0的值,就会将a所对应的元素排在前面。 var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; var byScore = function(a,b){ return b.score-a.score; }; students.sort(byScore); //arr.sort(); var studentNames = ["wq","xl","gp"]; studentNames.sort(); studentNames; // ["gp","wq","xl"] //arr.push();插到最后一个。 var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.push({id:4,score:90}); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.push({id:4,score:90},{id:5,score:60}); //arr.unshift;插到第一个。 var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.unshift({id:4,score:90}); //arr.shift();移除第一个并返回移除的值。 var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.shift(); // {id:1,score:80} //arr.pop();移除最后一个并返回移除的值。 var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.pop(); // {id:3,score:70} //arr.splice(移除的元素的索引号,移除的个数,插入的新元素); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.splice(1,1,{id:4,score:90}); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.splice(1,1); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; students.splice(1,0,{id:4,score:90}); //arr.slice(); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; var newStudents = students.slice(0,2); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; var newStudents = students.slice(0); //arr.contat(); var students1 = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; var students2 = [ {id:4,score:90}, {id:5,score:60} ]; var students3 = [ {id:6,score:40}, {id:7,score:30} ]; var newStudents = students1.concat(students2,students3); //arr.join(); var emails = ["wq@163.com","gp@163.com","xl@163.com"]; emails.join(";"); // "wq@163.com;gp@163.com;xl@163.com" //arr.forEach(); var scores = [60,70,80,90]; var newScores = []; var addScore = function(item,index,array){ newScores.push(item+5); }; scores.forEach(addScore); newScores; // [65,75,85,95] console.log(scores.forEach(addScore)); console.log(scores); //arr.map(); 有返回值 var scores = [60,70,80,90]; var addScore = function(item,index,array){ return item+5; }; scores.map(addScore); // [65,75,85,95] // arr.reduce(function(previousResult,item,index,array),previousResult的初始值); var students = [ {id:1,score:80}, {id:2,score:50}, {id:3,score:70} ]; var sum = function(previousResult,item,index,array){ console.log(previousResult); console.log(item); return previousResult+item.score; }; students.reduce(sum,0); // 200