Array类型
使用 instanceof
操作符检测某个对象是否为数组
var name = "hello word";
console.log(name instanceof Array);
instanceof
操作符问题在于,它假定单一的全局执行环境。如果网页中包含多个框架,那实际就存在俩个以上不同的全局执行环境,从而存在俩个不同版本的array构造函数。如果你从一个框架向另一个框架传入数组,那么传入的数组与在第二个框架中原生创建的数组分别具有各自不同的构造函数。
为了解决这个问题,ECMAScript5新增了 Array.isArray()方法。这个方法最终确定某个值是不是数字,而不管它是在哪个全局执行环境中创建的
var name = "hello word";
console.log(Array.isArray(name));
var array=["1","hello",3]
console.log(Array.isArray(array));
转换方法
调用数组的toString()
方法会返回由数组中每个值的字符串形式拼接而成的一个以逗号分隔的字符串。而调用valueOf()返回的还是数组。
var array = ["1", "hello", "3"];
console.log(array.toString());
console.log(array.valueOf());
// 输出:
// 1, hello, 3
// ['1', 'hello', '3']
默认情况下都会以逗号分隔的字符串形式返回数组项,如果使用join()
方法,则可以使用不同的分隔符构建这个字符串。join()方法只接收一个参数,即用作分隔符的字符串。
var array = ["1", "hello", "3"];
console.log(array.join("|"));
//1|hello|3
栈方法
栈是一种LIFO(Last-In-First_Out,后进先出)的数据结构,也就是最新添加的最早被移除。而栈中的项的插入(推入)和移除(弹出),只发生在一个位置——栈的顶部。ECMAScript
为数组专门提供了push
和pop
方法.
push()
方法可以接收任意数量的参数,把它们逐个添加到数组末尾,并返回数组的长度。
pop()
方法则从数组末尾移除最后一项,减少数组的length值,并返回移除的项。
var colors=["black","red"];
console.log(colors.push("orange"));//推入一项并返回数组长度=3
console.log(colors.pop());//取得最后一项
console.log(colors.length);//取得最后一项的长度
队列方法
队列数据结构访问规则是FIFO(First-In-First-Out,先进先出)。队列在列表的末端添加项,从列表的前端移除项。
push()
方法向数组末端添加项。
shift()
方法移除数组中的第一项并返回该项
var colors = ["black", "red"];
colors.push("orange");
colors.push("blue");//推入两项
console.log(colors.shift());//取得第一项
ECMAScript还未数组提供了一个unshift()
方法。它与shift()
方法的用途相反:它能在数组前端添加任意个项并返回数组长度。因此可以从相反的方向模拟队列。
unshift()
方法向数组的前端添加项
pop()
方法则从数组末端移除项
var colors = [];
colors.unshift("orange","blue");
colors.unshift("black"); //推入三项
console.log(colors);//输出:[ 'black', 'orange', 'blue' ]
console.log(colors.pop());//取得最后一项
console.log(colors);//输出:[ 'black', 'orange' ]
操作方法
- concat()
concat()方法可以基于当前数组中的所有项创建一个新数组。
var colors = [];
colors.unshift("orange", "blue");
colors.unshift("black");
colors.pop();
console.log(colors.concat("1", 2, 3)); //[ 'black', 'orange', '1', 2, 3 ]
- slice()
slice()方法,它能够基于当前数组中的一个或多个项创建一个数组。slice()方法接收一到两个参数,既要返回项的开始和结束为止。
var colors = [];
colors.unshift("orange", "blue");
colors.unshift("black");
colors.pop();
var newColors = colors.concat("1", 2, 3);
console.log(newColors.slice(2, 4));//[ '1', 2 ]
- splice()
splice()方法有很多种用法,它的主要用途是向数组的中部插入项,使用方式有三种:
删除:可以删除任意数量的项,需指定两个参数:要删除的项的起始为止和要删的项数。
插入:向指定的位置插入任意数量的项,需指定三个参数:起始位置、要删除的项数、和要插入的项。如果要插入多个项,可以在第三个参数后面以逗号隔开。
替换:向指定位置插入任意数量的项,且同时删除任意数量的项,需指定3个参数:起始位置,要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。
var colors = ["orange", "blue", "1", 2, 3];
//删除
colors.splice(0, 1);
console.log("删除:" + colors.toString());//删除:blue,1,2,3
//插入
colors.splice(1, 0, "a", "b", "c");
console.log("插入:" + colors.toString());//插入:blue,a,b,c,1,2,3
//替换
colors.splice(7, 0, "d").toString()
console.log("替换:" + colors.toString());//替换:blue,a,b,c,1,2,3,d
迭代方法
ECMAScript5为数组定义了5个迭代方法:
- every():对数组中的每一项运行给定函数,如果该函数对每一项都返回true,则返回true
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.every(function (item, index, array) {
return item > 2;
});
console.log(result);//false
- filter():对数组中的每一项运行给定函数,返回该函数会返回true的项组成的数组。
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.filter(function (item, index, array) {
return item > 2;
});
console.log(result);//[ 3, 4, 5, 6, 7, 8, 9 ]
- forEach():对数组中的每一项运行给定函数,没有返回值
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.map(function (item, index, array) {
return item * 2;
});
console.log(result); //[ 2, 4, 6, 8, 10, 12, 14, 16, 18, 0 ]
- some():对数组中的每一项运行给定函数,如果该函数对任何一项返回true,则返回true
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.some(function (item, index, array) {
return item >2;
});
console.log(result); //true
以上5个方法都不会改变数组本身的值。
缩小方法
ECMAScript 5 还新增了俩个缩小数组的方法:reduce()和reduceRight()。这俩个方法都会迭代数组的所有项,然后构建一个最终返回的值。其中,reduce()方法从数组的第一项开始,逐个遍历到最后。而reduceRight()则从数组的最后一项,向前遍历到第一项。
- reduce()方法
reduce()方法接收一个函数callbackfn作为累加器(accumulator),数组中的每个值(从左到右)开始合并,最终为一个值。
reduce()方法接收callbackfn函数,而这个函数包含四个参数:
function callbackfn(preValue,curValue,index,array){}
1.preValue: 上一次调用回调返回的值,或者是提供的初始值(initialValue)
2.curValue: 数组中当前被处理的数组项
3.index: 当前数组项在数组中的索引值
4.array: 调用 reduce()方法的数组。
5.而initialValue作为第一次调用 callbackfn函数的第一个参数。
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.reduce(function (perv, cur, idnex, array) {
return perv + cur;
});
console.log(result);//45
- reduceRight()方法
reduceRight()方法的功能和reduce()功能是一样的,不同的是reduceRight()从数组的末尾向前将数组中的数组项做累加 。
reduceRight()首次调用回调函数callbackfn时,prevValue 和 curValue 可以是两个值之一。如果调用 reduceRight() 时提供了 initialValue 参数,则 prevValue 等于 initialValue,curValue 等于数组中的最后一个值。如果没有提供 initialValue 参数,则 prevValue 等于数组最后一个值, curValue 等于数组中倒数第二个值。
示例:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.reduceRight(function (perv, cur, idnex, array) {
return perv + cur;
});
console.log(result);//45
如果提供一个初始值:
var numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var result = numbers.reduceRight(function (perv, cur, idnex, array) {
return perv + cur;
}, 10);
console.log(result); //55