JavaScript中Array 对象的一些常用API详解

concat()

描述:连接两个或更多的数组,并返回结果。

用法

array1.concat(array2,array3,...,arrayX)
参数描述
array2, array3, …, arrayX必需。该参数可以是具体的值,也可以是数组对象。可以是任意多个。

例子

var a= [222, 11];
var b= [33, 4, 5];
var c= [6];
var d = a.concat(b,c);
console.log(d) //[222, 11,33, 4, 5,6]

注意:该方法不会改变现有的数组,而仅仅会返回被连接数组的一个副本。

该方法可以用于分页数据的连接

every()

描述:检测数值元素的每个元素是否都符合条件,并返回true/false。

用法

array.every(function(currentValue,index,arr), thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}

console.log(ages.every(checkAdult));//false

every() 方法使用指定函数检测数组中的所有元素:

  • 如果数组中检测到有一个元素不满足,则整个表达式返回 false ,且剩余的元素不会再进行检测。
  • 如果所有元素都满足条件,则返回 true。

注意: every() 不会对空数组进行检测。
注意: every() 不会改变原始数组。

some()

描述:检测数组元素中是否有元素符合指定条件,并返回true/false。

用法

array.some(function(currentValue,index,arr), thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var ages = [32, 33, 16, 40];

function checkAdult(age) {
    return age >= 18;
}

console.log(ages.some(checkAdult));//true

some() 方法会依次执行数组的每个元素:

  • 如果有一个元素满足条件,则表达式返回true , 剩余的元素不会再执行检测。
  • 如果没有满足条件的元素,则返回false。

注意: some() 不会对空数组进行检测。
注意: some() 不会改变原始数组。

注意区分some()和every()的用法。

fill()

描述:使用一个固定值来替换数组的元素。

用法

array.fill(value, start, end)
参数描述
value必需。填充的值。
start可选。开始填充位置。
end可选。停止填充位置 (默认为 array.length)

例子

var a= [11, 22, 33, 44];

console.log(a.fill("2"));//["2", "2", "2", "2"]

注意: IE 11 及更早版本不支持 fill() 方法。

find()

描述:返回符合传入测试(函数)条件的数组元素(第一个元素)。

用法

array.find(function(currentValue, index, arr),thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var ages = [3, 10, 18, 20];
 
function checkAdult(age) {
    return age >= 18;
}
 
console.log(ages.find(checkAdult));//18

find() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, find() 返回符合条件的元素,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 undefined

注意: find() 对于空数组,函数是不会执行的。
注意: find() 并没有改变数组的原始值。
注意: IE 11 及更早版本不支持 find() 方法。

findIndex()

描述:返回符合传入测试(函数)条件的数组元素索引(第一个元素索引)。

用法

array.findIndex(function(currentValue, index, arr),thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var ages = [3, 10, 18, 20];
 
function checkAdult(age) {
    return age >= 18;
}
 
console.log(ages.find(checkAdult));//2

findIndex() 方法为数组中的每个元素都调用一次函数执行:

  • 当数组中的元素在测试条件时返回 true 时, findIndex() 返回符合条件的元素的索引位置,之后的值不会再调用执行函数。
  • 如果没有符合条件的元素返回 -1

注意: findIndex() 对于空数组,函数是不会执行的。
注意: findIndex() 并没有改变数组的原始值。
注意: IE 11 及更早版本不支持 find() 方法。

filter()

描述:检测数值元素,并返回符合条件所有元素的数组。

用法

array.filter(function(currentValue,index,arr), thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var ages = [3, 10, 18, 20];
 
function checkAdult(age) {
    return age >= 18;
}
 
console.log(ages.filter(checkAdult));//18,20

注意: filter() 不会对空数组进行检测。
注意: filter() 不会改变原始数组。

forEach()

描述:数组每个元素都执行一次回调函数。

用法

array.forEach(function(currentValue, index, arr), thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var num = [3, 10, 18, 20],
 sum = 0;
function myFunction(item) {
    sum += item;
}
 num.foreach(myFunction)
console.log(sum);//51

注意: forEach() 对于空数组是不会执行回调函数的。

map()

描述:通过指定函数处理数组的每个元素,并返回处理后的数组。

用法

array.map(function(currentValue, index, arr), thisValue)
参数描述
function(currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(currentValue, index,arr)

参数描述
currentValue必须。当前元素的值
index可选。当前元素的索引值
arr可选。当前元素属于的数组对象

例子

var num = [3, 10, 18, 20],
 sum = 0;
function myFunction(num) {
   return num*2;
}
 
console.log(num.map(myFunction));

注意: map() 不会对空数组进行检测。
注意: map() 不会改变原始数组。

reduce()

描述:将数组元素计算为一个值(从左到右)。

用法

array.reduce(function(total, currentValue, currentIndex, arr), initialValue)
参数描述
function(total,currentValue, index,arr)必须。函数,数组中的每个元素都会执行这个函数
thisValue可选。对象作为该执行回调时使用,传递给函数,用作 “this” 的值。 如果省略了 thisValue ,“this” 的值为 “undefined”

function(total,currentValue, index,arr)

参数描述
total必需。初始值, 或者计算结束后的返回值。
currentValue必需。当前元素
currentIndex可选。当前元素的索引
arr可选。当前元素属于的数组对象

例子

var num = [3, 10, 18, 20],
 sum = 0;
function getSum(total, num) {
    return total + num;
}
console.log(num.reduce(getSum));//3+10+18+20

reduce() 可以作为一个高阶函数,用于函数的 compose。
注意: reduce() 对于空数组是不会执行回调函数的。

reduceRight()

描述:将数组元素计算为一个值(从右到左),其他都与上一方法一致。

例子

var num = [3, 10, 18, 20],
 sum = 0;
function getSum(total, num) {
    return total - num;
}
console.log(num.reduceRight(getSum));//20-18-10-3

includes()

描述:判断一个数组是否包含一个指定的值。返回true/false

indexOf()

描述:搜索数组中的元素,并返回它最先出现的位置。不存在则返回-1

用法

array.indexOf(item,start)

lastIndexOf()

描述: 搜索数组中的元素,并返回它最后出现的位置。不存在则返回-1

用法

array.lastIndexOf(item,start)

isArray()

描述:判断对象是否为数组。返回true/false

    var fruits = ["Banana", "Orange", "Apple", "Mango"];
    console.log( Array.isArray(fruits));

join()

描述: 把数组的所有元素放入一个字符串。

	var fruits = ["Banana", "Orange", "Apple", "Mango"];
	console.log(fruits.join());

pop()

描述:删除数组的最后一个元素并返回删除的元素。

push()

描述:向数组的末尾添加一个或更多元素,并返回新的长度。

shift()

描述:删除并返回数组的第一个元素。

unshift()

描述:向数组的开头添加一个或更多元素,并返回新的长度。

slice()

描述:选取数组的的一部分,并返回一个新数组。

用法

array.slice(item,start)

例子

var fruits = ["Banana", "Orange", "Lemon", "Apple", "Mango"];
console.log( fruits.slice(-3,-1));

splice()

描述:从数组中添加或删除元素。

用法

array.splice(index,howmany,item1,.....,itemX)
参数描述
index必需。规定从何处添加/删除元素。该参数是开始插入和(或)删除的数组元素的下标,必须是数字。
howmany必需。规定应该删除多少元素。必须是数字,但可以是 “0”。如果未规定此参数,则删除从 index 开始到原数组结尾的所有元素。
item1, …, itemX可选。要添加到数组的新元素

例子

var fruits = ["Banana", "Orange", "Apple", "Mango"];//分别执行下面三行代码,可以再查询一下执行后fruits的数据内容是什么。
fruits.splice(2,0,"Lemon","Kiwi");//[]
fruits.splice(2,1,"Lemon","Kiwi");//["Apple"]
fruits.splice(2,2); //["Apple", "Mango"]

sort()

描述: 对数组的元素进行排序。

用法

array.sort(sortfunction)
参数描述
sortfunction可选。规定排序顺序。必须是函数。

例子

	var points = [40,100,1,5,25,10];
	points.sort(function(a,b){return b-a});

reverse()

描述: 反转数组的元素顺序。

toString()

描述: 把数组转换为字符串,并返回结果。

valueOf()

描述: 返回数组对象的原始值。可左类型的判断

本文章是本人学习了解所写,如有错误欢迎指出。
引用:https://www.runoob.com/jsref/jsref-obj-array.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值