1.join()
var colors = ["red", "green", "blue"];alert(colors.join(",")); //red,green,bluealert(colors.join("||")); //red||green||blue
如果不给join()方法传入任何值,或者给它传入undefined,那么则使用逗号作为分隔符。
如果数组中的某一项的值是null或者undefined,那么在join()方法返回的结果中以空字符串表示。
var colors = ["red", "green", "blue",null];colors.join("+");//"red+green+blue+"var colors = ["red", "green", "blue",null,undefined];"red+green+blue++"
2.push()和pop()
var colors = new Array(); // 创建一个数组var count = colors.push("red", "green"); // 推入两项alert(count); //2count = colors.push("black"); // 推入另一项alert(count); //3var item = colors.pop(); // 取得最后一项alert(item); //"black"alert(colors.length); //2
3.shift()和unshift()
var colors = new Array(); //创建一个数组var count = colors.unshift("red", "green"); //推入两项alert(count); //2count = colors.unshift("black"); //推入另一项alert(count); //3var item = colors.pop(); //取得最后一项alert(item); //"green"alert(colors.length); //2 console.log(colors.shift()) //"black" console.log(colors) //"red"
4.reverse()
var values = [1, 2, 3, 4, 5];values.reverse();alert(values); //5,4,3,2,1
5.sort()
sort()方法可以接收一个比较函数作为参数,以便我们指定哪个值位于哪个值的前面。
比较函数接收两个参数,如果第一个参数应该位于第二个之前则返回一个负数,如果两个参数相等则返回0,如果第一个参数应该位于第二个之后则返回一个正数。以下就是一个简单的比较函数:
//升序 function compare(value1, value2) { if (value1 < value2) { return -1; } else if (value1 > value2) { return 1; } else { return 0; }}
var values = [0, 1, 5, 10, 15];
values.sort(compare); alert(values); //0,1,5,10,15//降序function compare(value1, value2) { if (value1 < value2) { return 1; } else if (value1 > value2) { return -1; } else { return 0; }}var values = [0, 1, 5, 10, 15];values.sort(compare);alert(values); // 15,10,5,1,0
6.concat()
var colors = ["red", "green", "blue"];var colors2 = colors.concat("yellow", ["black", "brown"]);alert(colors); //red,green,bluealert(colors2); //red,green,blue,yellow,black,brown
7.slice()。
slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。在只有一个参数的情况下,slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。如果有两个参数,该方法返回起始和结束位置之间的项——但不包括结束位置的项。
注:slice()方法不会影响原始数组
var colors = ["red", "green", "blue", "yellow", "purple"];var colors2 = colors.slice(1);var colors3 = colors.slice(1,4);alert(colors2); //green,blue,yellow,purplealert(colors3); //green,blue,yellow
8.splice()
a.删除:指定两个参数--->要删除的第一项的位置和要删除的项数
var colors = ["red", "green", "blue"];var removed = colors.splice(0,1); // 删除第一项alert(colors); // green,bluealert(removed); // red,返回的数组中只包含一项
b.插入:提供三个参数:起始位置、要删除的项数和要插入的项数
removed = colors.splice(1, 0, "yellow", "orange"); // 从位置1 开始插入两项alert(colors); // green,yellow,orange,bluealert(removed); // 返回的是一个空数组
c.替换:提供三个参数:起始位置、要删除的项数和要插入的项数
removed = colors.splice(1, 1, "red", "purple"); // 插入两项,删除一项alert(colors); // green,red,purple,orange,bluealert(removed); // yellow,返回的数组中只包含一项
9.indexOf()和lastIndexOf()
这两个方法都接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。
var numbers = [1,2,3,4,5,4,3,2,1];alert(numbers.indexOf(4)); //3alert(numbers.lastIndexOf(4)); //5alert(numbers.indexOf(4, 4)); //5alert(numbers.lastIndexOf(4, 4)); //3var person = { name: "Nicholas" };var people = [{ name: "Nicholas" }];var morePeople = [person];alert(people.indexOf(person)); //-1alert(morePeople.indexOf(person)); //0
参考资料
《javascript高级程序设计(第3版)》第5章 引用类型