一、Array
1、栈方法eg:
var colors = [ "red", "green", "black" ];
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple", "pear");
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple");
document.write("colors: " + colors);
document.write("<br>");
colors.pop();
document.write("colors: " + colors);
2、队列方法。eg:
var colors = [ "red", "green", "black" ];
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple", "pear");
document.write("colors: " + colors);
document.write("<br>");
colors.push("apple");
document.write("colors: " + colors);
document.write("<br>");
var shi = colors.shift();
document.write("colors.shift() : " + shi);
document.write("<br>");
document.write("colors: " + colors);
2、排序方法sort
var arrs = [ {
age : 11,
name : "liyong"
}, {
age : 2,
name : "ahangsan"
} ]
function compareFun(arg) {
return function(obj1, obj2) {
var v1 = obj1[arg];
var v2 = obj2[arg];
if (v1 > v2) {
return 1;
} else if (v1 < v2) {
return -1;
} else {
return 0
}
}
}
console.log("arrs :",arrs);
var sort_arrys = arrs.sort(compareFun("name"));//
if(sort_arrys===arrs){
document.write("sort方法的返回值和排序前的数组名指向同一对象");
}
console.log("arrs.sort(compareFun(name)); :",arrs);
3、操作的方法。
contact方法,首先复制当前数组的副本,然后把参数中每一项逐一添加到副本中,此方法不会影响原来的数组。
var colors = [ "red", "green", "black" ];
document.write("colors: " + colors);
document.write("<br>");
var contactColor = colors.concat("gray",["key","value"])
document.write("After concat ");
document.write("<br>");
document.write("colors: " + colors);
document.write("<br>");
document.write("contactColor: " + contactColor);
document.write("<br>");
slice 方法会基于当前数组创建新数组,可以接受一个活两个参数,表示新数组在当前数组的起始位置(和结束位置),此方法不影响原数组。
var colors = [ "red", "green", "black", "gray" ];
document.write("colors: " + colors);
document.write("<br>");
var sliceColor1 = colors.slice(1)
var sliceColor2 = colors.slice(0, 2)
document.write("=======After slice ==========");
document.write("<br>");
document.write("colors: " + colors);
document.write("<br>");
document.write("colors.slice(1): " + sliceColor1);
document.write("<br>");
document.write("colors.slice(0,2) : " + sliceColor2);
document.write("<br>");
数组可以使用splice删除、插入、替换元素。eg.
var colors = [ "red", "green", "black", "gray" ];
document.write("colors: " + colors);
document.write("<br>");
document.write("=======After splice ==========");
document.write("<br>");
colors.splice(1, 1)
document.write("删除数据: colors.splice(1, 1)" + colors);
document.write("<br>");
colors.splice(0, 0,"lisi")
document.write("插入数据: colors.splice(0,0, lisi)" + colors);
document.write("<br>");
colors.splice(2, 1,"zhangsan")
document.write("替换数据: colors.splice(2, 1,zhangsan)" + colors);
document.write("<br>");
4、迭代方法。
ervery():对数组中的每一项运行给定函数,如果该函数的每一项返回true,则该方法返回true.
filter():对数组中每一项运行给定函数,返回该函数会返回true的项组成的数组。
forEach():对数组中的每一项运行给定的函数,没有返回值。
map():对数组中的每一项运行给定的函数,返回每次函数调用返回结果的数组。
var num =[1,2,3,4];
var filter = num.filter(function(item,index,array){
return item>2;
});
//num==array
var map = num.map(function(item,index,array){
return item+2*3;
});
document.write("filter : "+ filter);
document.write("<br>");
document.write("map : "+ map);
document.write("<br>");
5、reduce方法
var num =[1,2,3,4,5];
var sum = num.reduce(function(pre,cur,index,array){
document.write("pre : "+ pre+" cur : "+cur+" index:"+index);
document.write("<br>");
return pre+cur;
});
//array == num
var map = num.map(function(item,index,array){
return item+2*3;
});
document.write("sum : "+ sum);
document.write("<br>");
二、Regexp
1、var expression = /pattern/flags;
var expression = new RegExp(pattern,flags)
g:应用与所有的字符串,而非发现第一个就停止匹配。
i:不区分大小写。
m:表示多行(multiline)模式,到达一行末尾的时候还会找下一行中是否从在匹配的项。
Regpex的每个实例都有下列属性。
global(boolean),表示是否设置了g标志。
ignoreCase(boolean),表示是否设置了i标志。
lastindex表示下一个匹配项的字符位置,从0算起。
multiline m
source 正则表达式中的pattern.
var pattern = /\[bc\]at/gi;
document.write("global: " + pattern.global);
document.write("<br>");
document.write("source: " + pattern.source);
document.write("<br>");
document.write("multiline: " + pattern.multiline);
document.write("<br>");
document.write("lastIndex: " + pattern.lastIndex);
document.write("<br>");
document.write("ignoreCase: " + pattern.ignoreCase);
document.write("<br>");
2、exec方法,如果patten设置了全局变量g也只返回一个匹配项,但是对于同一字符串多次调用会继续查找新的匹配项,而如果不设置g,则多次调用exec()始终返回第一项的匹配信息
var pattern = /mom (and dady (and baby)?)?/gi;
var matches = pattern.exec(text);
console.log("matches: ",matches);
本文详细介绍了JavaScript中的数组操作方法,包括栈方法、队列方法、排序方法等,并通过实例展示了如何使用这些方法。同时,文章还介绍了正则表达式的使用方法及其属性。
858

被折叠的 条评论
为什么被折叠?



