浅谈数组的原型方法

目录

at()

concat()

copyWithin()

entries()

every() 

fill()

filter()

find()

findIndex()

findLast() 

findLastIndex() 

flat()

flatMap()

forEach()

includes()


at()

说明:

一般获取数组中最后一位元素会采用以下方法:

let a =[1,2,3,4,5,6];
console.log(a[a.length-1]);//6

现在使用at()方法可以更方便地获取:

console.log(a.at(-1)); //6

并且 at() 方法里面的参数 >=0 时也可以正常使用:

console.log(a.at(5)); //6

但是当参数大于等于数组长度时,会返回undefined:

console.log(a.at(6)); //undefined

可以说使用 at() 简化了获取数组末尾元素的代码,如填写的负数 "-2" 可以理解成方法自动帮你计算 arr.length-2。

兼容性:

concat()

说明:

用于连接两个或多个数组;返回值是拼接后的数组,不改变原数组的值

let a = [1, 2, 3, 4, 5, 6];
let b = [7, 8, 9, 10, 11, 12];

let result1=a.concat(b);
console.log(result1); //[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]

兼容性: 

copyWithin()

说明:

copyWithin(target,start,end)

target:必须

start:可选,默认0

end:可选,默认等于数组长度

可以复制数组的一部分(该一部分默认是从数组第一个到最后一个元素)粘贴到规定了的数组的起始位置,并返回改变后的数组。该方法会改变原数组的内容,但是不会改变数组的长度。

 target(必需):代表粘贴数据时数组起始下标,也就是从哪里开始粘贴内容:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(2);
console.log("从第三个位置开始替换:", a); //[1, 2, 1, 2, 3, 4]

 当target大于数组长度时,则不会替换:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(10);
console.log("target大于数组长度:", a); //[1, 2, 3, 4, 5, 6]

 当target为负数时,代表从数组倒数第几个开始粘贴内容:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(-2);
console.log("代表从数组倒数第二个开始粘贴内容:", a); //[1, 2, 3, 4, 1, 2]

 start(可选):从该位置开始复制数据:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(2, 4);
console.log("从数组下标为4的位置开始复制:", a); //[1, 2, 5, 6, 5, 6]

 当start为负数时,复制的起始点从数组的末尾开始计算:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(2, -2);
console.log("从数组倒数第二个开始复制", a); //[1, 2, 5, 6, 5, 6]

 end(可选),到该位置停止复制数据:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(2, 1, 4);
console.log("在数组下标为4的数据前停止复制:", a); //[1, 2, 2, 3, 4, 6]

 end为负数时,代表复制数据的终点从数组末尾开始计算:

let a = [1, 2, 3, 4, 5, 6];
a.copyWithin(2, 1, -2);
console.log("复制到数组倒数第2个数据前", a); //[1, 2, 2, 3, 4, 6]

兼容性:

entries()

说明:

entries() 方法返回一个数组的迭代对象,该对象包含数组的键值对 (key/value)。

其中key代表数组的索引值,value代表索引值对应的元素。

此方法并不会改变原数组

下面使用for...of对其进行遍历输出

let a = [1, 2, 3, 4, 5, 6];
for(let [index,val] of a.entries()){
    console.log(index,val);
}
// 输出:
// 0 1
// 1 2
// 2 3
// 3 4
// 4 5
// 5 6

 

every() 

说明:

用于遍历检测数组所有元素是否都符合指定条件(通过函数提供)。

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

如果所有元素都满足条件,则返回 true。

注意: every() 不会对空数组进行检测,并直接返回true

注意: every() 不会改变原始数组。

语法:

array.every(function(currentValue,index,arr), thisValue)

currentValue(必需):当前元素的值

index(可选):对应的索引值

arr(可选):被遍历的数组

thiaValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"。例如填入另一个数组b,则this指向数组b。

let a = [1, 2, 3, 4, 5, 6];
let b = [7, 8, 9, 10, 11, 12];
let result2 = a.every(function (item, index, arr) {
    console.log(this); //[7, 8, 9, 10, 11, 12]
    return item > 0;
},b);
console.log(result2); //true

fill()

说明:

用于将一个固定值替换数组内的元素,会改变原数组

语法:

array.fill(value, start, end)

value(必需):填充的值。

start(可选):开始填充的位置

end(可选):停止填充的位置 (默认为 array.length)

let a = [1, 2, 3, 4, 5, 6];
a.fill(11,1,5);
console.log("fill()方法填充后的结果:",a); //[1, 11, 11, 11, 11, 6]

 兼容性:

filter()

说明:

用于过滤一个数组,规定过滤条件,结果返回一个符合条件的新数组,不会改变原数组。

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

语法:

array.filter(function(currentValue,index,arr), thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
let result = a.filter((item) => {
  return item > 2;
});
console.log(result); //[3, 4, 5, 6]

兼容性: 

find()

说明:

find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。

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

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

注意: find() 对于空数组,函数是不会执行的,并且返回的是undefined。

注意: find() 并没有改变数组的原始值。

 语法:

array.find(function(currentValue, index, arr),thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
let result4 = a.find((item) => {
  return item > 3;
});
console.log(result4); // 4

兼容性: 

findIndex()

说明:

findIndex() 方法返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

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

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

注意: findIndex() 对于空数组,函数是不会执行的,返回值为-1。

注意: findIndex() 并没有改变数组的原始值。

 语法:

array.findIndex(function(currentValue, index, arr),thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
let result5 = a.findIndex((item) => {
  return item > 3;
});
console.log(result5); // 3

兼容性: 

findLast() 

说明:

findLast() 方法和find()方法相反,是从数组最后一个元素开始遍历,然后返回通过测试(函数内判断)的数组的第一个元素的值。

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

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

注意: findLast() 对于空数组,函数是不会执行的,并且返回的是undefined。

注意: findLast() 并没有改变数组的原始值。

 语法:

array.findLast(function(currentValue, index, arr),thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
let result6 = a.findLast((item) => {
  return item < 3;
});
console.log(result6); // 2

 兼容性:

findLastIndex() 

说明:

findLastIndex() 方法和findIndex()方法相反,是从数组最后一个元素开始遍历,然后返回传入一个测试条件(函数)符合条件的数组第一个元素位置。

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

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

注意: findLastIndex() 对于空数组,函数是不会执行的,返回值为-1。

注意: findLastIndex() 并没有改变数组的原始值。

 语法:

array.findLastIndex(function(currentValue, index, arr),thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
let result7 = a.findLastIndex((item) => {
  return item < 3;
});
console.log(result7); // 1

兼容性: 

flat()

说明: 

能将嵌套数组转一维数组,默认转换一层,如果需要转换更多层,那么需要传入参数;

方法不会改变原数组,会返回一个新数组;

语法:

 array.flat(string)

string(可选):代表转换几层,默认1层

let c=[1,[2,[3,[4,5]]]];

let result8 = c.flat();
console.log(result8); //[1,2,[3,[4,5]]]

let result9 = c.flat(2);
console.log(result9); //[1,2,3,[4,5]]

//自动跳过空位
let d =[1, ,[2,[3,[4,5]]]];
let result10 = d.flat();
console.log(result10); //[1,2,[3,[4,5]]]

兼容性:

 

flatMap()

 说明:

遍历数组的每一项,每一项的返回值由我们自行决定,可以试原数组元素,或是进行加减乘除运算后的,或是符合某个条件的,或是其他自定义的值,最后返回一个新数组,不改变原数组。

语法:

 array.flatMap(function(currentValue, index, arr),thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
let result11 = a.flatMap((item) => {
  return item * 2;
});
console.log(result11); //[2, 4, 6, 8, 10, 12]

兼容性:

 

forEach()

说明: 

遍历数组,对每个元素进行操作,没有返回值,不会改变原数组。

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

 语法:

array.forEach(function(currentValue, index, arr), thisValue)

currentValue(必需):当前元素的值

index(可选):当前元素的索引值

arr(可选):当前元素属于的数组对象

thisValue(可选):对象作为该执行回调时使用,传递给函数,用作 "this" 的值。
如果省略了 thisValue ,"this" 的值为 "undefined"

let a = [1, 2, 3, 4, 5, 6];
a.forEach((item) => {
  item = item * 2;
  console.log(item);
 console.log(a);
});
// 2
// [1, 2, 3, 4, 5, 6]
// 4
// [1, 2, 3, 4, 5, 6]
// 6
// [1, 2, 3, 4, 5, 6]
// 8
// [1, 2, 3, 4, 5, 6]
// 10
// [1, 2, 3, 4, 5, 6]
// 12
// [1, 2, 3, 4, 5, 6]

兼容性: 

includes()

说明:

用来判断一个数组是否包含一个指定的值,如果是返回 true,否则false,不改变原数组。

语法:

 arr.includes(searchElement, fromIndex)

 searchElement(必须):需要查找的元素值。

 fromIndex(可选):从该索引处开始查找 searchElement。如果为负值,则按升序从 array.length + fromIndex 的索引开始搜索。默认为 0。

let a = [1, 2, 3, 4, 5, 6];
let result12 = a.includes(2);
console.log(result12); // true

result12 = a.includes(2, -3);
console.log(result12); // false

兼容性:

 

indexOf() 

说明:

未完待续。。。。 

 

join()

说明: 

用于把数组中的所有元素转换一个字符串。不改变原数组。

语法:

array.join(separator)

separator(可选):指定要使用的分隔符。如果省略该参数,则使用逗号作为分隔符。

let a = [1, 2, 3, 4, 5, 6];
let result13 = a.join();
console.log(result13) // 1,2,3,4,5,6

result13 = a.join("-");
console.log(result13) // 1-2-3-4-5-6

兼容性:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值