prototype
toString将数组转成字符串,逗号分隔
filter创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素
array.filter(function(currentValue,index,arr), thisValue);
注意: filter() 不会对空数组进行检测。
注意: filter() 不会改变原始数组
参数二thisValue: 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"
find返回通过测试(函数内判断)的数组的第一个元素的值
array.find(function(currentValue, index, arr),thisValue);
注意: find() 对于空数组,函数是不会执行的。
注意: find() 并没有改变数组的原始值
参数二thisValue: 可选。对象作为该执行回调时使用,传递给函数,用作 "this" 的值。如果省略了 thisValue ,"this" 的值为 "undefined"
findIndex返回传入一个测试条件(函数)符合条件的数组第一个元素位置
同上
forEach
array.forEach(function(currentValue, index, arr), thisValue);
from用于通过拥有 length 属性的对象或可迭代的对象来返回一个新数组
Array.from(object, mapFunction, thisValue);
参数 |
描述 |
object |
必需,要转换为数组的对象。 |
mapFunction |
可选,数组中每个元素要调用的函数。 |
thisValue |
可选,映射函数(mapFunction)中的 this 对象。 |
eg:var newArr = Array.from([1, 2, 3], x => x * 2);
//判断是否是数组 if(arr instanceof Array) if(Array.isArray(arr))
//验证是否是数组(前提是toString没有被重写过)
function isArray(o) {
return Object.prototype.toString.call(o) == “[object Array]”;
}
concat用于连接两个或多个数组
arr1.concat(arr2); //concat并不会改变arr1本身,它会返回一个新的数组
Array.prototype.push.apply(arr1,arr2);
includes用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false
arr.includes(searchElement, fromIndex);
indexOf
lastIndexOf
array.indexOf(item, fromIndex);
isArray判断是否是数组
Array.isArray(arr);
join用于把数组中的所有元素转换一个字符串
var str = array.join(separator); //默认逗号作为分隔符,返回一个字符串
keys用于从数组创建一个包含数组键的可迭代对象
var keys = arr.keys(); //
eg: var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x = fruits.keys();
document.getElementById("demo1").innerHTML = x.next().value; 0
document.getElementById("demo2").innerHTML = x.next().value; 1
document.getElementById("demo3").innerHTML = x.next().value; 2
map返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值
array.map(function(currentValue,index,arr), thisValue);
注意: map() 不会对空数组进行检测。
注意: map() 不会改变原始数组
pop用于删除数组的最后一个元素并返回删除的元素
var ele = array.pop();
shift移除数组第一个元素并返回删除的元素
var ele = array.shift();
push向数组的末尾添加一个或多个元素,并返回新的长度
var newLen = array.push(item1, item2, ..., itemX);
unshift 在数组起始位置添加元素,并返回新的长度
var newLen = array.unshift(item1,item2, ..., itemX);
reduce接收一个函数作为累加器,数组中的每个值(从左到右)开始缩减,最终计算为一个值
var sum = array.reduce(function(total, currentValue, currentIndex, arr), initialValue);
注意: reduce() 对于空数组是不会执行回调函数的
reduceRight从右向左
reverse颠倒数组中元素的顺序,直接修改原数组
array.reverse();
slice可从已有的数组中返回选定的元素,可提取字符串的某个部分,并以新的字符串返回被提取的部分
var newArr = array.slice(start, end); //并不会改变原数组
参数 |
描述 |
start |
可选。规定从何处开始选取。如果是负数,那么它规定从数组尾部开始算起的位置。也就是说,-1 指最后一个元素,-2 指倒数第二个元素,以此类推。 |
end |
可选。规定从何处结束选取。该参数是数组片断结束处的数组下标。如果没有指定该参数,那么切分的数组包含从 start 到数组结束的所有元素。如果这个参数是负数,那么它规定的是从数组尾部开始算起的元素。 |
将类数组转成数组
Array.prototype.slice.call(argements)
splice添加或删除数组中的元素,会改变原有数组
array = array.splice(index, howmany, ele1, ele2, ...);
参数 |
描述 |
index |
必需。规定从何处添加/删除元素。 |
howmany |
可选。规定应该删除多少元素。必须是数字,但可以是 "0"。 |
item1, ..., itemX |
可选。要添加到数组的新元素 |
some用于检测数组中是否存在某元素满足指定条件(函数提供)
array.some(function(currentValue,index,arr),thisValue); //返回true或false
some() 方法用于检测数组中的元素是否满足指定条件(函数提供)。
some() 方法会依次执行数组的每个元素:
如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
如果没有满足条件的元素,则返回false。
注意: some() 不会对空数组进行检测。
注意: some() 不会改变原始数组。
sort用于对数组的元素进行排序,会改变原始数组
array.sort(sortfunction)