转换方法
- toString()返回一个以逗号分隔的字符串,不改变原数组。
- valueOf()返回一个以逗号分隔的与原数组一样的值,不改变原数组。
- toLocaleString()方法经常会返回与1,2方法相同的值。不同的是当数组调用此方法时是调用每一项值的toLocaleString(),而不是toString()方法。不改变原数组。
var colors=["red","blue","green"];
alert(colors.toString());//red,blue,green 字符串
alert(colors.valueOf());//red,blue,green 数组
alert(colors.toLocaleString());//red,blue,green 字符串
栈方法与队列方法
- push()方法接任意量的参数,逐个添加到数组或字符串的末尾,并返回添加后的数组的length,改变原数组。
- pop()方法移除数组或字符串的末尾项,返回移除的项,改变原数组。
- unshift()方法接任意量的参数,逐个添加到数组或字符串的前端,并返回添加后的数组的length,改变原数组。
- shift()方法移除数组或字符串的第一项,返回移除的项,改变原数组。
<script type="text/javascript">
//**栈方法**
var colors = new Array();//create an array
var count = colors.push("red", "green");
//push two items
alert(count); //2
count = colors.push("black");//push another item on
alert(count); //3
var item = colors.pop(); //get the last item
alert(item); //"black"
alert(colors.length); //2
alert(colors);//red, green
//**队列方法**
var item-2 = colors.shift(); //get the first item
alert(item-2); //"red"
alert(colors.length); //1
var count-2 = colors.unshift('yellow','maroon');
//push another item on
alert(count-2); //3
alert(colors.length); //3
alert(colors);//yellow,maroon,green
</script>
排序方法
- reverse();反转数组项的顺序,改变原数组。
- sort();对数组重新排序,升序或降序,改变原数组。
var values = [1, 2, 3, 4, 5];
values.reverse();
alert(values); //5,4,3,2,1
function compare(value1, value2) {
if (value1 < value2) {
return 1;
} else if (value1 > value2) {
return -1;
} else {
return 0;
}
}
//升序或降序取决于谁减谁
/*function compare(value1, value2) {
return value2-value1;
}*/
var values = [0, 1, 5, 10, 15];
values.sort(compare);
alert(values); //15,10,5,1,0
操作方法
- concat();创建一个当前的数组的副本,接收任意的字符串或者数组,逐一添加到结果数组的末尾,返回新的结果数组。如果没有添加任意的参数则返回原数组的副本。不改变原数组
var colors = ["red", "green", "blue"];
var colors2 = colors.concat("yellow", ["black", "brown"]);
alert(colors);//red,green,blue
alert(colors2);//red,green,blue,yellow,black,brown
splice();强大的数组方法,主要用于中部插入项,改变原数组
- 删除:可以删除任意数量的项,只需指定两个参数:要删除的第一项的起始位置和要删除的项数;
- 插入:可以向指定的位置插任意数量的项,指定三个参数:
要插入的起始位置,0(不删除任意一项),插入的项(有几项就插入几个参数); - 替换:可以向指定的位置替换任意的项,且同时删除任意数量的项,指定三个参数:要替换的起始位置,要删除的项数和要插入任意数量的项。插入的项数不一定与删除的项数相等。例如:splice(2,1,’red’,’green’),会删除当前数组位置2的一项,然后再从位置2开始插入’red’和’green’。
var colors = ["red", "green", "blue"];
var removed = colors.splice(0,1);
//remove the first item
alert(colors); //green,blue
alert(removed); //red - one item array
removed = colors.splice(1, 0, "yellow", "orange");
//insert two items at position 1
alert(colors); //green,blue,yellow,orange
alert(removed); //empty array
removed = colors.splice(1, 1, "red", "purple");
//insert two values, remove one
alert(colors); //green,red,purple,orange
alert(removed); //yellow - one item array
- slice();能够基于当前数组中的一个或多个项创建一个新的数组,可以接收一个或两个参数,即要返回的起始位置和结束位置。一个参数时:返回指定的位置到末尾项,两个参数时:返回指定位置到结束位置之间的项(不包括结束位置项);不改变原数组
- substring();substr();slice();这三个方法的参数个数相同,并且第一个参数的使用方法相同,substring()与slice()第二个参数的使用方法相同,而substr()的第二个参数意思是指定要返回的字符个数。不改变原数组
参数是负数的情况:
1. slice()当参数(不管是一个或两个)是负数时:将负数与数组长度相加得到新的指定参数;
2. substring()当参数(不管是一个或两个)是负数时:将负数转换为0;
3. substr()当第一个参数为负数时:将负数与数组长度相加;当第二个参数为负数时:将负数转换为0。
var stringValue = "hello world";
alert(stringValue.slice(3)); //"lo world"
alert(stringValue.substring(3)); //"lo world"
alert(stringValue.substr(3)); //"lo world"
alert(stringValue.slice(3, 7)); //"lo w"
alert(stringValue.substring(3,7)); //"lo w"
alert(stringValue.substr(3, 7)); //"lo worl"
alert(stringValue.slice(-3)); //"rld"
alert(stringValue.substring(-3)); //"hello world"
alert(stringValue.substr(-3)); //"rld"
alert(stringValue.slice(3, -4)); //"lo w"
alert(stringValue.substring(3, -4));
//"hel"第二个参数小于第一个参数的可以转换在前面
alert(stringValue.substr(3, -4));
//"" (empty string)第二个参数小于第一个参数的不可以转换在前面,返回空字符串
位置方法
- indexOf()和lastIndexOf();可以接受一个或两个参数;即要查找的项或字符,查找的起始位置(可选);indexOf()从前端开始向后查找,lastIndexOf()从末尾往前查找。找到即返回该项的位置索引,找不到即返回-1
var numbers = [1,2,3,4,5,4,3,2,1];
alert(numbers.indexOf(4)); //3
alert(numbers.lastIndexOf(4)); //5
alert(numbers.indexOf(4, 4)); //5
alert(numbers.lastIndexOf(4, 4)); //3
var person = { name: "Nicholas" };
var people = [{ name: "Nicholas" }];
var morePeople = [person];
alert(people.indexOf(person)); //-1
alert(morePeople.indexOf(person)); //0
var stringValue = "hello world";
alert(stringValue.indexOf("o")); //4
alert(stringValue.lastIndexOf("o")); //7
alert(stringValue.indexOf("o", 6)); //7
alert(stringValue.lastIndexOf("o", 6)); //4
var stringValue-2 = "Lorem ipsum dolor sit ame consectetur adipisicing elit";
var positions = new Array();
var pos = stringValue.indexOf("e");
while(pos > -1){
positions.push(pos);
pos = stringValue-2.indexOf("e", pos + 1);
}
alert(positions); //"3,24,32,35,52"
总结
- 转换方法、concat()、slice()、substring()、substr()、位置方法均不改变原数组。
- 栈方法和队列方法、splice()、排序方法均改变原数组。