Array.isArray(obj)
function validate (a){
Array.isArray(arguments); //返回:false
Array.isArray(a); //返回true
}
var arr = [1, 2, 3];
validate(arr);
Array.prototype.shift()[ECMAScript 3]
var arr = ['a', 'b'];
arr.shift(); // arr:['b'],返回'a'
Array.prototype.unshift(elem1?, elem2?, ...)[ECMAScript 3]
var arr = ['a', 'b'];
arr.unshift('c',['d', 'e', 'f']); // arr:["c", ["d", "e", "f"], "a", "b"], 返回:4
Array.prototype.pop()[ECMAScript 3]
var arr = ['a', 'b'];
arr.pop(); // arr:['a'],返回'b'
Array.prototype.push([elem1, elem2, ...])[ECMAScript 3]
var arr = ['a', 'b'];
arr.push('c',['d', 'e', 'f']); // arr:["a", "b", "c", ["d", "e", "f"]], 返回:4
Array.prototype.splice(start[, deleteCount, elem1, elem2, ...])[ECMAScript 3]
var arr = ['a', 'b', 'c', 'd', 'e'];
arr.splice(3); //从第三位开始,不指定第二个参数。 arr:['a', 'b', 'c'], 返回:["d", "e"]
// arr:['a', 'b', 'c']
arr.splice(1, 1, 'f', 'g', 'h'); //从第一位开始,删除一个元素,插入三个元素。 arr:["a", "f", "g", "h", "c"], 返回['b']
// arr:["a", "f", "g", "h", "c"]
arr.splice(0, 0, 'x', 'y', 'z'); // arr:["x", "y", "z", "a", "b", "c", "d", "e"], 返回:[]
Array.prototype.reverse()[ECMAScript 3]
var arr = ['a', 'b', 'c'];
arr.reverse(); //arr:['c', 'b', 'd'],返回['c', 'b', 'd']
Array.prototype.sort([compareFunction])[ECMAScript 3]
// 不传递比较函数
var arr = ['apple', 'banana', 'pear', 'orange'];
arr.sort(); //arr:["apple", "banana", "orange", "pear"],返回:["apple", "banana", "orange", "pear"]
// 不传递比较函数:数字
var arr = [-1, -20, 50, 7];
arr.sort(); //arr:[-1, -20, 50, 7],返回:[-1, -20, 50, 7]
// 传递比较函数
几种常见比较 - 比较数字
function compareCanonically(a, b){
return a < b ? -1 : (a > b ? 1 : 0);
}
var arr = [-1, -20, 50, 7];
arr.sort(compareCanonically); // arr:[-20, -1, 7, 50],返回:[-20, -1, 7, 50]
- 比较字符串:可以使用String.prototype.localeCompare
var arr = ['c', 'a', 'b'];
arr.sort(function(a , b){ return a.localeCompare(b);}); // arr:["a", "b", "c"],返回:["a", "b", "c"]
- 比较对象
var arr = [
{name: 'Tarzan'},
{name: 'Cheeta'},
{name: 'Jane'}];
function compareNames(a, b){
return a.name.localeCompare(b.name);
}
arr.sort(compareNames); // arr:[{name: 'Cheeta'}, {name: 'Jane'}, {name: 'Tarzan'}].返回同arr
var arr = [
{name: 'Tarzan', age: 39},
{name: 'Cheeta', age: 79},
{name: 'Jane', age: 19}];
function compareAge(a, b){
return a.age < b.age ? 1 : (a.age > b.age ? -1 : 0);
}
arr.sort(compareAge); // arr:[{name: 'Cheeta', age: 79}, {name: 'Tarzan', age: 39}, {name: 'Jane', age: 19}].返回同arr
Array.prototype.concat([arr1, arr2, ...])[ECMAScript 3]
var arr = ['a', 'b'];
arr.concat('c',['d', 'e']); // arr:["a", "b"],返回:["a", "b", "c", "d", "e"]
Array.prototype.slice([begin, end])[ECMAScript 3]
var arr = ['a', 'b', 'c', 'd'];
arr.slice(1, 3); // 返回["b", "c"]
// 省略结束位置
arr.slice(1); // 返回["b", "c", "d"]
// 没有传递参数:实现copy
arr.slice(); // 返回:['a', 'b', 'c', 'd']
// 只使用一个负数(-2+4=2),从index为2开始截取
arr.slice(-2); // 返回:["c", "d"]
arr.slice(1, -1); // 返回:["b", "c"]
Array.prototype.join([separator])[ECMAScript 3]
注意点
Array.join()方法会将数组中的空洞也转化为空字符串。
理解空洞:可以有两种方式创建一个hole
// 方式一
var arr = [];
arr[0] = 'a';
arr[2] = 'c';
1 in arr; // false, hole at index 1
arr.length; //3
// 方式二
var arr = ['a',,'c'];
1 in arr; //false, hole at index 1
arr.length; //3
// 如果最后一位是hole则会被忽略
var arr = ['a',];
arr.length; // 1
var arr = ['a',,];
arr.length; // 2
用法
var arr = [3, 4, 5];
arr.join(); // 返回:3,4,5
arr.join('-'); // 返回:3-4-5
arr.join(''); //返回:345
// 包含undefined和null
var arr = [3, undefined, 5, null, 7];
arr.join('-'); // 返回:3--5--7
// 包含hole
var arr = [3,, 5];
arr.join('-'); // 返回:3--5
Array.prototype.indexOf(searchValue[, startIndex])[ECMAScript 5]
var arr = ['a', 'b', 'c', 'd', 'a'];
arr.indexOf('a',0); //返回:0
arr.indexOf('a',1); //返回:4
arr.indexOf('a',-1); //返回:4
arr.indexOf('b',-1); // 返回:-1
// true:'1' == 1; false: '1' === 1
var arr = ['1', 1, '2'];
arr.indexOf(1); //返回:1
Array.prototype.lastIndexOf(searchElement[, startIndex])[ECMAScript 5]
var arr = ['b', 'a', 'c', 'd', 'a'];
arr.lastIndexOf('a',0); //返回:-1
arr.lastIndexOf('a',1); //返回:1
arr.lastIndexOf('a',-1); //返回:4
arr.lastIndexOf('c',-3); // 返回:2
Array.prototype.forEach(callback[, thisValue])[ECMAScript 5]
var arr = [1, 2, 3, 4];
var sum = 0;
arr.forEach(function(elem){
sum += elem;
});
sum; //10
Array.prototype.every(callback[, thisValue])[ECMAScript 5]
function isEven(x){
return x % 2 === 0;
}
var arr = [1, 3, 4];
arr.every(isEven); //返回:false
var evenArr = [2, 4, 6];
evenArr.every(isEven); //返回:true
var arr = [];
arr.every(isEven); //返回:true
Array.prototype.some(callback[, thisValue])[ECMAScript 5]
function isEven(x){
return x % 2 === 0;
}
var arr = [1, 3, 4];
arr.some(isEven); //返回:true
var arr = [];
arr.some(isEven); //返回:false
Array.prototype.map(callback[, thisValue])[ECMAScript 5]
var arr = [1, 2, 3, 4];
arr.map(function(elem){
return 2*elem;
}); //返回:[2, 4, 6, 8]
Array.prototype.filter(callback[, thisValue])[ECMAScript 5]
// 压缩稀疏数组(arr)的空缺
var arr = ['a', ,'c', 'd'] // ["a", 2: "c", 3: "d"]
arr.filter(function(){
return true;
}); // 返回:['a', 'c', 'd']
// 压缩或者删除undefined和null元素
var arr = ['a', 'b', undefined, null];
arr.filter(function(elem){
return elem !== undefined && elem != null;
}); // 返回:['a', 'b']
Array.prototype.reduce(callback[, initialValue])[ECMAScript 5]
function add(prev, cur){
return prev + cur;
}
var arr = [10, 3, -1];
arr.reduce(add); //返回:12
var arr = [1];
arr.reduce(add); // 返回:1
var arr = [];
arr.reduce(add, 1); // 返回:1
Array.prototype.reduceRight(callback[, initialValue])[ECMAScript 5]
function pow(pre, cur){
return Math.pow(pre, cur);
}
var arr = [1, 2, 3];
arr.reduce(pow); //返回:1((1^2)^3)
arr.reduceRight(pow); //返回:9((3^2)^1)
Array.prototype.toString()[ECMAScript 3]
var arr = ['a', 'b', 'c', 'd'];
arr.toString(); //返回:a,b,c,d
Array.prototype.toLocaleString()[ECMAScript 3]
Array.from(arrayLike[, mapFunc, thisArg])
<body>
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
</ul>
<script>
var liEle = document.getElementsByTagName('li');
Array.from(liEle).forEach(function(item, index){
item.innerHTML = 'array-' + (index + 1);
});
</script>
</body>
Array.of(item0, item1, ...)
注意点
Array(); // [],length:0
Array(3); // [],length:3
Array(3, 11, 8); // [3, 11, 8]
浏览器兼容性
用法
Array.of(); // []
Array.of(3); // [3]
Array.of(3, 11, 8); // [3, 11, 8]
用法
"use strict";
var indexArr = ['a', 'b'].keys(); // 迭代对象
indexArr.next().value; // 0
indexArr.next().value; // 1
let letter = ['a', 'b', 'c'];
let entries = letter.entries();
entries.next().value; // [0, 'a']
entries.next().value; // [1, 'b']
entries.next().value; // [2, 'c']
Array.prototype.find(predicate[, thisArg])
var arr = [6, -5, 8];
arr.find(function(value){
return value < 0;
}); //返回-5
arr.find(function(value){
return value > 10;
}); //返回undefined
var arr = [6, -5, 8, NaN];
arr.find(function(value){
return Object.is(NaN, value);
}); //返回NaN
Array.prototype.findIndex(predicate[, thisArg])
var arr = [6, -5, 8, NaN];
arr.findIndex(function(value){
return Object.is(NaN, value);
}); //返回3
arr.findIndex(function(value){
return value > 10;
}); //返回-1
Array.prototype.includes(value, start)
[1, 2, 3].includes(3,-3); // 返回:true
[1, 2, 3].includes(3,-5); //返回:true
[1, 2, 3].includes(4); // 返回:false
[1, 2, NaN].includes(NaN); // 返回:true
Array.prototype.copyWithin(target, start, end)
var arr = [1, 2, 3, 4, 5];
// 从index=3开始查找元素,到index=4.则查找元素为4,5.从index为0开始替换
arr.copyWithin(0, 3, 5); //返回:[4, 5, 3, 4, 5];arr:[4, 5, 3, 4, 5]
Array.prototype.fill(value, start, end)
var arr = [1, 2, 3, 4, 5];
arr.fill('*', 1, 2); //返回:[1, "*", 3, 4, 5],arr:[1, "*", 3, 4, 5]
arr.fill(); //返回:[undefined, undefined, undefined, undefined, undefined],arr一样