1.转换方法
toLocaleString()、toString()和 valueOf()方法
var colors = ["red", "blue", "green"]; // 创建一个包含 3 个字符串的数组
console.log(colors.toString()); // red,blue,green
console.log(colors.valueOf()) //["red", "blue", "green"]
console.log(colors); // ["red", "blue", "green"]
调用数组的 toString()方法会返回由数组中每个值的字符串形式拼接而成的一个以逗号分隔的字符串。而
调用 valueOf()返回的还是数组
2.栈方法
push()和 pop()方法
数组可以表现得就像栈一样,后者是一种可以限制插入和删除项的数据结构。栈是一种 LIFO(Last-In-First-Out,
后进先出)的数据结构,也就是最新添加的项最早被移除。而栈中项的插入(叫做推入)和移除(叫做
弹出),只发生在一个位置——栈的顶部
push()方法可以接收任意数量的参数,把它们逐个添加到数组末尾,并返回修改后数组的长度
pop()方法则从数组末尾移除最后一项,减少数组的 length 值,然后返回移除的项
var colors = new Array(); // 创建一个数组
var count = colors.push("red", "green"); // 推入两项
alert(count); //2
count = colors.push("black"); // 推入另一项
alert(count); //3
var item = colors.pop(); // 取得最后一项
alert(item); //"black"
alert(colors.length); //2
3.队列方法
shift()和 push()方法
队列数据结构的访问规则是 FIFO(First-In-First-Out,先进先出)。队列在列表的末端添加项,从列表的前端移除项
shift(),它能够移除数组中的第一个项并返回该项,同时将数组长度减 1
unshift() 它能在数组前端添加任意个项并返回新数组的长度
结合使用 shift()和 push()方法,可以像使用队列一样使用数组
var colors = new Array(); //创建一个数组
var count = colors.push("red", "green"); //推入两项
alert(count); //2
count = colors.push("black"); //推入另一项
alert(count); //3
var item = colors.shift(); //取得第一项
alert(item); //"red"
alert(colors.length); //2
使用 unshift()和 pop()方法,可以从相反的方向来模拟队列,即在数组的前端添加项,从数组末端移除项
var colors = new Array(); //创建一个数组
var count = colors.unshift("red", "green"); //推入两项
alert(count); //2
count = colors.unshift("black"); //推入另一项
alert(count); //3
var item = colors.pop(); //取得最后一项
alert(item); //"green"
alert(colors.length); //2
。。。未完待续