ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

ES6中copyWithin()与fill()的不同之处

2021-10-10 13:32:42  阅读:172  来源: 互联网

标签:zeroes ES6 console log copyWithin 索引 ints fill


复制和填充方法

ES6新增了两个方法:批量复制方法copyWithin(),以及填充数组方法fill()。这两个方法的函数签名类似,都需要指定既有数组实例上的一个范围,包含开始索引,不包含结束索引。使用这个方法不会改变数组的大小。
使用fill()方法可以向一个已有的数组中插入全部或部分相同的值。开始索引用于指定开始填充的位置,它是可选的。如果不提供结束索引,则一直填充到数组末尾。负值索引从数组末尾开始计算也可以将负值索引想象成数组长度加上它得到的一个正索引

      const zeroes = [0, 0, 0, 0, 0];
      // 用5填充整个数组
      zeroes.fill(5);
      console.log(zeroes); // [5, 5, 5, 5, 5]
      // 重置
      zeroes.fill(0);
      console.log(zeroes); // [0, 0, 0, 0, 0]
	  const zeroes = [0, 0, 0, 0, 0];
	  // 用6填充索引大于等于3的元素
      zeroes.fill(6, 3);
      console.log(zeroes); //[0, 0, 0, 6, 6]
      const zeroes = [0, 0, 0, 0, 0];
	  // 用7填充索引大于等于1且小于3的元素
      zeroes.fill(7, 1, 3);
      console.log(zeroes); // [0, 7, 7, 0, 0]
      const zeroes = [0, 0, 0, 0, 0];
      // 用8填充索引大于等于1且小于4的元素
      // (-4 + zeroes.length = 1)
      // (-1 + zeroes.length = 4)
      zeroes.fill(8, -4, -1);
      console.log(zeroes); //[0, 8, 8, 8, 0]
      const zeroes = [0, 0, 0, 0, 0];
      //fill() 静默忽略超出数组边界、零长度及方向相反的索引范围
      //索引过低,忽略
      zeroes.fill(1, -10, -6);
      console.log(zeroes); // [0, 0, 0, 0, 0]
      // 索引过高,忽略
      zeroes.fill(1, 10, 15);
      console.log(zeroes); // [0, 0, 0, 0, 0]
      // 索引反向,忽略
      zeroes.fill(2, 4, 2);
      console.log(zeroes); // [0, 0, 0, 0, 0]
      // 索引部分可用,填充可用部分
      zeroes.fill(4, 3, 10);
      console.log(zeroes); //[0, 0, 0, 4, 4]

fill()不同,copyWithin()会按照指定范围浅复制数组中的部分内容,然后将它们插入到指定索引开始的位置。开始索引和结束索引则于fill()使用同样的计算方法

      let ints,
      	  reset = () => ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
      reset();
      // 从ints中复制索引0开始的内容,插入索引5开始的位置
      // 在源索引或目标索引到达数组边界时停止
      ints.copyWithin(5);
      console.log(ints); //[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
      reset();
      // 从ints中复制索引5开始的内容,插入到索引0开始的位置
      ints.copyWithin(0, 5);
      console.log(ints); //[5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
      reset();
      // 从ints中复制索引0开始到索引3结束的内容
      // 插入到索引4开始的位置
      ints.copyWithin(4, 0, 3);
      console.log(ints); // [0, 1, 2, 3, 0, 1, 2, 7, 8, 9]
      reset();
       // JavaScript 引擎在插值前会完全复制范围内的值
      // 因此复制期间不存在重写的风险
      ints.copyWithin(2, 0, 6);
      console.log(ints); // [0, 1, 0, 1, 2, 3, 4, 5, 8, 9]
      reset();
      // 支持负索引值,与fill()相对于数组末尾计算正向索引的过程是一样的
      ints.copyWithin(-4, -7, -3);
      console.log(ints); // [0, 1, 2, 3, 4, 5, 3, 4, 5, 6]
	  // copyWithin() 静默忽略超出数组边界、零长度及方向相反的索引范围
      let ints,
          reset = () => (ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
      reset();
      //   索引过低,忽略
      ints.copyWithin(1, -15, -12);
      console.log(ints); //  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      reset();
      //   索引过高,忽略
      ints.copyWithin(1, 12, 15);
      console.log(ints); //  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      reset();
      //   索引反向,忽略
      ints.copyWithin(2, 4, 2);
      console.log(ints); //  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      reset();
      //   索引部分可用,复制、填充可用部分
      ints.copyWithin(4, 7, 10);
      console.log(ints); // [0, 1, 2, 3, 7, 8, 9, 7, 8, 9]

标签:zeroes,ES6,console,log,copyWithin,索引,ints,fill
来源: https://blog.csdn.net/KOBE_24VS8/article/details/120684091

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有