1:concat():数组a合并数组b,a+b
var aAry = [1, 2, 4];
var bAry = [1, 3, 5];
console.log(aAry.concat(bAry));
//打印:[1, 2, 4, 1, 3, 5];
2:join():数组转字符串
var cAry = [1, 2, 3];
console.log(cAry.join('a'));
//打印:[1a2a3];
3:pop():删除最后一个元素,会修改原数组
var dAry = [1, 2, 3, 4];
console.log(dAry.pop());
console.log(dAry);
//打印:4
//打印:[1, 2, 3];
4:push():向数组末尾添加一个或多个元素,会修改原数组
var eAry = [1, 2, 3, 4];
console.log(eAry.push(6, 7)); //返回数组的长度
console.log(eAry);
//打印:6
//打印:[1, 2, 3, 4, 6, 7];
5:unshift():向数组开头添加一个或多个元素,会修改原数组
var fAry = [1, 2, 3, 4];
console.log(fAry. unshift(0, 1, 0)); //返回数组的长度
console.log(fAry);
//打印:7
//打印:[0, 1, 0, 1, 2, 3, 4];
6:shift(): 删除并返回第一个元素,会修改原数组
var gAry = [1, 2, 3, 4];
console.log(gAry. shift()); //返回删除的元素
console.log(gAry);
//打印:1
//打印:[2, 3, 4];
7:reverse():颠倒数组顺序
var hAry = [1, 2, 3, 4];
console.log(hAry. reverse());
//打印:[4, 3, 2, 1];
8:slice(startIndex, endIndex):截取数组,不修改原数组
var jAry = [1, 2, 3, 4, 5, 1, 3];
console.log(jAry. slice(1, 3));
console.log(jAry.);
//打印:[2, 3];
//打印:[1, 2, 3, 4, 5, 1, 3];
9:sort():排序,修改原数组
var kAry = [1, 2, 3, 4, 5, 1, 3];
console.log(kAry. sort());
//打印:[1, 1, 2, 3, 3, 4, 5];
10:splice():最强大的方法,插入,替换,删除元素,修改原数组
//插入元素
var lAry = [1, 2, 3, 4];
lAry.splice(1, 0, 'a') //在第一个元素之前插入元素
console.log(lAry);
//打印:[1, a, 2, 3, 4];
//替换元素
lAry. splice(1, 1, 'b') //替换第一个元素
console.log(lAry);
//打印:[1, b, 2, 3, 4];
//删除元素splice(startIndex, length)
lAry. splice(1, 1); //删除第一个元素
console.log(lAry);
//打印:[1, 2, 3, 4];
11:every():对数组中每一项进行给定函数,如果每一项都返回true,则返回true
var numbers = [1, 2, 3, 4, 5, 6, 7];
var everyResult = numbers.every(function (item, index, array) {
// body...
return item > 1;
});
console.log(everyResult);
//打印:false;
12:some():对数组中每一项进行给定函数,如果任意一项都返回true,则返回true
var numbers = [1, 2, 3, 4, 5, 6, 7];
var someResult = numbers. some(function (item, index, array) {
// body...
return item > 1;
});
console.log(someResult);
//打印:true;
13:filter():对数组中每一项进行给定函数,返回该函数会返回true的项组成的数组
var numbers = [1, 2, 3, 4, 5, 6, 7];
var filteResult = numbers.filter(function(item, index, array){
return item > 2;
});
console.log(filteResult);
//打印:[ 3, 4, 5, 6, 7 ];
14:map():对数组中每一项进行给定函数,返回每次函数调用的结果组成的数组
var numbers = [1, 2, 3, 4, 5, 6, 7];
var mapResult = numbers.map(function (item, index, array) {
// body...
return item * 2;
})
console.log(mapResult);
//打印:[ 2, 4, 6, 8, 10, 12, 14 ];
15:instanceof 判断类型是否正确
var numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(numbers instanceof Array);
console.log(Array.isArray(numbers));
console.log(typeof numbers);
//打印:true
//打印:true
//打印:object
16:indexOf():获取元素位置
var numbers = [1, 2, 3, 4, 5, 6, 7];
console.log(numbers.indexOf(4));
console.log(numbers.lastIndexOf(0)); //没有找到此元素则返回-1
//打印:3
//打印:-1
网友评论