常用的Array总结(包含ES5/6/7/8/9/10)

本文详细介绍了JavaScript中Array对象的各种方法,包括改变原数组的方法如`copyWithin`, `fill`, `pop/push/reverse/shift/unshift/sort/splice`,不改变原数组的方法如`concat/join/slice/indexOf/lastIndexOf/includes`等,以及ES6新增的迭代方法如`forEach/every/some/filter/map/reduce`等,最后还涵盖了ES6至ES10的新特性如`find/findIndex/fill/entries/keys/values/flat/flatMap`等。通过本文,读者可以全面了解和掌握Array的各种操作技巧。" 85200635,89843,Solidity哈希函数使用详解:sha256与keccak256,"['Solidity', '以太坊', '智能合约', '哈希算法', '区块链开发']
摘要由CSDN通过智能技术生成

第一部分介绍数组的定义和访问

 1.  数组定义

  • 使用new操作符来定义 new Array
let arr = new Array();
  •  使用 [ ] 定义 数组字面量法
let arr = [];

 2.  数组赋值

  • 使用数组字面量法赋值 (常用)
let arr = ['a', 'b', 'c'];

 3.  值的访问

  • 通过数组下标的索引访问
let arr = ['a', 'b', 'c'];
let item = arr[0]; // a
  • 数组解构 [ES6新增] 
let colors = [ "red", "green", "blue" ];
let [ firstColor, secondColor ] = colors; // 按照逗号计算索引 第一个逗号出现前面的元素的索引是0
console.log(firstColor); // "red"
console.log(secondColor); // "green"

let colors = [ "red", [ "green", "lightgreen" ], "blue" ];
let [ firstColor, [ secondColor ] ] = colors;
console.log(firstColor); // "red"
console.log(secondColor); // "green"

 

第二部分介绍在原有的Array对象上进行操作的方法,调用了这些方法之后会改变原始数组

   1. [ES6新增]  arr.copyWithin(target[, start[, end]]) 指定位置的成员复制到其他位置(会覆盖原有成员,返回修改后的数组

  • target 0 为基底的索引,复制序列到该位置。如果是负数,target 将从末尾开始计算。
  • start(非必传) 0 为基底的索引,开始复制元素的起始位置。如果是负数,start 将从末尾开始计算。如果 start 被忽略,copyWithin 将会从0开始复制
  • end (非必传)0 为基底的索引,开始复制元素的结束位置。copyWithin 将会拷贝到该位置,但不包括 end 这个位置的元素。如果是负数, end 将从末尾开始计算。如果 end 被忽略,copyWithin 方法将会一直复制至数组结尾(默认为 arr.length

    let arr = ['a', 'b', 'c', 'd', 'e'];
    //三种参数都存在 分别为 0 1 3 实现的效果是将数组中索引为1-3的元素这里为b c复制到从索引为0开始的位置进行复制 即a b将被b c替代
    arr.copyWithin(0, 1, 3);
    console.log(arr); // [ 'b', 'c', 'c', 'd', 'e' ]
    
    let arr1 = ['a', 'b', 'c', 'd', 'e'];
    //俩种参数都存在 分别为 0 1 实现的效果是将数组中索引为1-末尾的元素这里为b c d e复制到从索引为0开始的位置进行复制 即a b c d将被b c d e替代
    arr1.copyWithin(0, 1);
    console.log(arr1); // [ 'c', 'c', 'd', 'e', 'e' ]
    
    let arr2 = ['a', 'b', 'c', 'd', 'e'];
    //只有一种参数存在为 2 实现的效果是将数组中索引为0-末尾的元素这里为a b c d e复制到从索引为2开始的位置进行复制 即b c d将被a b c替代(需要注意的是不能改变数组的长度 如果超出则省略)
    arr2.copyWithin(2);
    console.log(arr2); // [ 'c', 'c', 'd', 'e', 'e' ]
    
    注意:每次重复定义arr的原因是因为此方法属于操作会改变原数组

   2. arr.fill(value[, start[, end]]) 返回修改后的数组

  • value 用来填充数组元素的值。
  • start(非必传)可选 起始索引,默认值为0。
  • end(非必传) 可选 终止索引,默认值为 this.length。
let arr = ['a', 'b', 'c', 'd', 'e'];
//三种参数都存在 分别为 0 1 3 实现的效果是将数组中索引为1-3的元素替换为0
arr.fill(0, 1, 3);
console.log(arr); // [ 'a', 0, 0, 'd', 'e' ]

let arr1 = ['a', 'b', 'c', 'd', 'e'];
//俩种参数都存在 分别为 0 1 实现的效果是将数组中索引为1-末尾的元素这里为b c d e替换为0
arr1.fill(0, 1);
console.log(arr1); // [ 'a', 0, 0, 0, 0 ]

let arr2 = ['a', 'b', 'c', 'd', 'e'];
//只有一种参数存在为 2 实现的效果是将数组中索引为0-末尾的元素替换为2
arr2.fill(2);
console.log(arr2); // [ 2, 2, 2, 2, 2 ]

  3.  arr.pop() 从一个数组中删除并返回最后一个元素 返回删除的这个数组

let arr = ['a', 'b'];
let item_pop = arr.pop();
console.log(arr); // [ 'a' ]
console.log(item_pop); // b

  4. arr.push() 将一个或多个元素添加到数组的末尾,并返回该数组的新长度 

let arr = ['a', 'b', 'c', 'd', 'e'];
let arr_length = arr.push('f');
console.log(arr); // [ 'a', 'b', 'c', 'd', 'e', 'f' ]
console.log(arr_length); // 6
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值