js中最常用到的数据集合,熟练掌握这些方法可以提高我们的工作效率

  js 数组添加对象_js给对象数组添加元素_js对象添加数组

  数组js 中最常用到的数据集合,其内置的方法有很多,熟练掌握这些方法,可以有效的提高我们的工作效率,同时对我们的代码质量也是有很大影响。

  一、创建数组1.使用数组字面量表示法

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr4 = [];   //创建一个空数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr5 = [20];   // 创建一个包含1项数据为20的数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr6 = ["lily","lucy","Tom"];   // 创建一个包含3个字符串的数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  2.使用 Array 构造函数无参构造

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr1 = new Array();   //创建一个空数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  带参构造

  如果只传一个数值参数,则表示创建一个初始长度为指定数值的空数组

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr2 = new Array(20);   // 创建一个包含20项的数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  如果传入一个非数值的参数或者参数个数大于 1,则表示创建一个包含指定元素的数组

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr3 = new Array("lily","lucy","Tom");   // 创建一个包含3个字符串的数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var array4 = new Array('23'); // ["23"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  3.Array.of 方法创建数组(es6 新增)

  ES6 为数组新增创建方法的目的之一,是帮助开发者在使用 Array 构造器时避开 js 语言的一个怪异点。

  Array.of()方法总会创建一个包含所有传入参数的数组js 数组添加对象,而不管参数的数量与类型。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">let arr = Array.of(1, 2);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.length);//2<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />let arr1 = Array.of(3);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr1.length);//1<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr1[0]);//3<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />let arr2 = Array.of('2');<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2.length);//1<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2[0]);//'2'<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  4.Array.from 方法创建数组(es6 新增)

  在 js 中将非数组对象转换为真正的数组是非常麻烦的。在 ES6 中,将可迭代对象或者类数组对象作为第一个参数传入js 数组添加对象,Array.from()就能返回一个数组。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">function arga(...args) {  //...args剩余参数数组,由传递给函数的实际参数提供<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    let arg = Array.from(args);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    console.log(arg);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arga('arr1', 26, 'from'); // ['arr1',26,'from']<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  映射转换

  如果你想实行进一步的数组转换,你可以向 Array.from()方法传递一个映射用的函数作为第二个参数。此函数会将数组对象的每一个值转换为目标形式,并将其存储在目标数组的对应位置上。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">function arga(...args) {  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />     return Array.from(args, value => value + 1);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />let arr = arga('arr', 26, 'pop');<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);//['arr1',27,'pop1']<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  如果映射函数需要在对象上工作,你可以手动传递第三个参数给 Array.from()方法,从而指定映射函数内部的 this 值

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">const helper = {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />  diff: 1,<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />  add(value) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    return value + this.diff;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />  }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />function translate() {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> //arguments 是一个对应于传递给函数的参数的类数组对象<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />  return Array.from(arguments, helper.add, helper); <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />let arr = translate('liu', 26, 'man');<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr); // ["liu1", 27, "man1"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  二、数组方法数组原型方法主要有以下这些各个方法的基本功能详解1.join()

  join()方法用于把数组中的所有元素转换一个字符串。

  元素是通过指定的分隔符进行分隔的。默认使用逗号作为分隔符

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1,2,3];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.join());   // 1,2,3<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.join("-"));   // 1-2-3<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   // [1, 2, 3](原数组不变)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  通过join()方法可以实现重复字符串,只需传入字符串以及重复的次数,就能返回重复后的字符串,函数如下:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">function repeatString(str, n) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />//一个长度为n+1的空数组用string去拼接成字符串,就成了n个string的重复<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> return new Array(n + 1).join(str);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(repeatString("abc", 3));   // abcabcabc<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(repeatString("Hi", 5));   // HiHiHiHiHi<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  2.push()和 pop()

  push()方法从数组末尾向数组添加元素,可以添加一个或多个元素。

  pop()方法用于删除数组的最后一个元素并返回删除的元素。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = ["Lily","lucy","Tom"];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var count = arr.push("Jack","Sean");<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(count);  // 5<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   // ["Lily", "lucy", "Tom", "Jack", "Sean"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var item = arr.pop();<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(item);   // Sean<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   // ["Lily", "lucy", "Tom", "Jack"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  3.shift() 和 unshift()

  shift()方法用于把数组的第一个元素从其中删除,并返回第一个元素的值。

  unshift()方法可向数组的开头添加一个或更多元素,并返回新的长度。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = ["Lily","lucy","Tom"];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var count = arr.unshift("Jack","Sean");<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(count);   // 5<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   //["Jack", "Sean", "Lily", "lucy", "Tom"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var item = arr.shift();<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(item);   // Jack<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   // ["Sean", "Lily", "lucy", "Tom"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  4.sort()

  sort() 方法用于对数组的元素进行排序。

  排序顺序可以是字母或数字,并按升序或降序。

  默认排序顺序为按字母升序。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr1 = ["a", "d", "c", "b"];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr1.sort());   // ["a", "b", "c", "d"]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr2 = [13, 24, 51, 3];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2.sort());   // [13, 24, 3, 51]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2);   // [13, 24, 3, 51](元数组被改变)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  为了解决上述问题,sort()方法可以接收一个比较函数作为参数,以便我们指定哪个值位于哪个值的前面。

  比较函数接收两个参数,如果第一个参数应该位于第二个之前则返回一个负数,如果两个参数相等则返回 0,如果第一个参数应该位于第二个之后则返回一个正数。以下就是一个简单的比较函数:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">function compare(value1, value2) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    if (value1  value2) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />     return 1;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    } else {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />     return 0;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr2 = [13, 24, 51, 3];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2.sort(compare));   // [3, 13, 24, 51]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  如果需要通过比较函数产生降序排序的结果,只要交换比较函数返回的值即可:

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">function compare(value1, value2) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    if (value1  value2) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />     return -1;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    } else {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />     return 0;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />    }<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr2 = [13, 24, 51, 3];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2.sort(compare));   // [51, 24, 13, 3]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  5.reverse()

  reverse() 方法用于颠倒数组中元素的顺序。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [13, 24, 51, 3];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.reverse());   //[3, 51, 24, 13]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   //[3, 51, 24, 13](原数组改变)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  6.concat()

  concat() 方法用于连接两个或多个数组。

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

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1,3,5,7];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arrCopy = arr.concat(9,[11,13]);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy);   //[1, 3, 5, 7, 9, 11, 13]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   // [1, 3, 5, 7](原数组未被修改)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  从上面测试结果可以发现:传入的不是数组,则直接把参数添加到数组后面,如果传入的是数组,则将数组中的各个项添加到数组中。但是如果传入的是一个二维数组呢?

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arrCopy2 = arr.concat([9,[11,13]]);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy2);   //[1, 3, 5, 7, 9, Array[2]]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy2[5]);   //[11, 13]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  7.slice()

  slice():返回从原数组中指定开始下标到结束下标之间的项组成的新数组。

  slice()方法可以接受一或两个参数,即要返回项的起始和结束位置。

  在只有一个参数的情况下, slice()方法返回从该参数指定位置开始到当前数组末尾的所有项。

  如果有两个参数,该方法返回起始和结束位置之间的项,但不包括结束位置的项。

  当出现负数时,将负数加上数组长度的值(6)来替换该位置的数

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1,3,5,7,9,11];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arrCopy = arr.slice(1);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arrCopy2 = arr.slice(1,4);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arrCopy3 = arr.slice(1,-2);//相当于arr.slice(1,4)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arrCopy4 = arr.slice(-4,-1);//相当于arr.slice(2,5)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   //[1, 3, 5, 7, 9, 11](原数组没变)<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy);   //[3, 5, 7, 9, 11]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy2);   //[3, 5, 7]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy3);   //[3, 5, 7]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrCopy4);   //[5, 7, 9]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  8.splice()

  splice():很强大的数组方法,它有很多种用法,可以实现删除、插入和替换。

  ######## 1.删除元素,并返回删除的元素

  可以删除任意数量的项,只需指定 2 个参数:要删除的第一项的位置和要删除的项数。例如, splice(0,2)会删除数组中的前两项。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1,3,5,7,9,11];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arrRemoved = arr.splice(0,2);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);   //[5, 7, 9, 11]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arrRemoved);   //[1, 3]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  ######## 2.向指定索引处添加元素

  可以向指定位置插入任意数量的项,只需提供 3 个参数:起始位置、 0(要删除的项数)和要插入的项。例如,splice(2,0,4,6)会从当前数组的位置 2 开始插入 4 和 6。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var array1 = [22, 3, 31, 12];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />array1.splice(1, 0, 12, 35);  //[]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(array1); // [22, 12, 35, 3, 31, 12]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  ######## 3.替换指定索引位置的元素

  可以向指定位置插入任意数量的项,且同时删除任意数量的项,只需指定 3 个参数:起始位置、要删除的项数和要插入的任意数量的项。插入的项数不必与删除的项数相等。例如,splice (2,1,4,6)会删除当前数组位置 2 的项,然后再从位置 2 开始插入 4 和 6。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">const array1 = [22, 3, 31, 12];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />array1.splice(1, 1, 8);   //[3]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(array1);  // [22, 8, 31, 12]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  js对象添加数组_js给对象数组添加元素_js 数组添加对象

  9.indexOf()和 lastIndexOf()

  接收两个参数:要查找的项和(可选的)表示查找起点位置的索引。

  indexOf():从数组的开头(位置 0)开始向后查找。

  lastIndexOf:从数组的末尾开始向前查找。

  这两个方法都返回要查找的项在数组中的位置,或者在没找到的情况下返回-1。在比较第一个参数与数组中的每一项时,会使用全等操作符。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1,3,5,7,7,5,3,1];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.indexOf(5));   //2<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.lastIndexOf(5));   //5<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.indexOf(5,2));   //2<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.lastIndexOf(5,4));   //2<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr.indexOf("5"));   //-1<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  10.forEach()

  forEach():对数组进行遍历循环,对数组中的每一项运行给定函数。这个方法没有返回值。参数都是 function 类型,默认有传,。

  参数分别为:遍历的数组内容;第对应的数组索引,数组本身

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [11, 22, 33, 44, 55];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr.forEach(function(x, index, a){<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> console.log(x + '|' + index + '|' + (a === arr));<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />});<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />输出为:<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> 11|0|true<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> 22|1|true<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> 33|2|true<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> 44|3|true<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> 55|4|true<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  11.map()

  map()方法返回一个新数组,数组中的元素为原始数组元素调用函数处理后的值。

  map()方法按照原始数组元素顺序依次处理元素。

  该方法不会改变原数组

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1, 2, 3, 4, 5];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr2 = arr.map(function(item){<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> return item*item;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />});<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2);  //[1, 4, 9, 16, 25]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  12.filter()

  filter():“过滤”功能,数组中的每一项运行给定函数,返回满足过滤条件组成的数组。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr2 = arr.filter(function(x, index) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> return index % 3 === 0 || x >= 8;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />});<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2);  //[1, 4, 7, 8, 9, 10]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  13.fill() es6 新增

  fill()方法能使用特定值填充数组中的一个或多个元素。当只是用一个参数时,该方法会用该参数的值填充整个数组。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">let arr = [1, 2, 3, 'cc', 5];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr.fill(1);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);//[1,1,1,1,1];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  如果不想改变数组中的所有元素,而只是想改变其中一部分,那么可以使用可选的起始位置参数与结束位置参数(不包括结束位置的那个元素)

  3 个参数:填充数值,起始位置参数,结束位置参数(不包括结束位置的那个元素)

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">let arr = [1, 2, 3, 'arr', 5];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr.fill(1, 2);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);//[1,2,1,1,1]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr.fill(0, 1, 3);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);//[1,0,0,1,1];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  js给对象数组添加元素_js对象添加数组_js 数组添加对象

  14.every()

  every():判断数组中每一项都是否满足条件,只有所有项都满足条件,才会返回 true。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">var arr = [1, 2, 3, 4, 5];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr2 = arr.every(function(x) {<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /> return x  2;<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />})); // 2 返回匹配位置的索引<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  20.copyWithin() [es6 新增]

  copyWithin() 方法用于从数组的指定位置拷贝元素到数组的另一个指定位置中。

  该方法会改变现有数组

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">//将数组的前两个元素复制到数组的最后两个位置<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />let arr = [1, 2, 3, 'arr', 5];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr.copyWithin(3, 0);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);//[1,2,3,1,2]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  默认情况下,copyWithin()方法总是会一直复制到数组末尾,不过你还可以提供一个可选参数来限制到底有多少元素会被覆盖。这第三个参数指定了复制停止的位置(不包含该位置本身)。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">let arr = [1, 2, 3, 'arr', 5, 9, 17];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />//从索引3的位置开始粘贴<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />//从索引0的位置开始复制<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />//遇到索引3时停止复制<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr.copyWithin(3, 0, 3);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr);//[1,2,3,1,2,3,17]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  21.flat() 和 flatMap() es6 新增

  flat() 方法会按照一个可指定的深度递归遍历数组,并将所有元素与遍历到的子数组中的元素合并为一个新数组返回。

  该方法返回一个新数组,对原数据没有影响。

  参数:指定要提取嵌套数组的结构深度,默认值为 1。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">const arr1 = [0, 1, 2, [3, 4]];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr1.flat());<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// expected output: [0, 1, 2, 3, 4]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />const arr2 = [0, 1, 2, [[[3, 4]]]];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(arr2.flat(2));<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// expected output: [0, 1, 2, [3, 4]]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />//使用 Infinity,可展开任意深度的嵌套数组<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr4 = [1, 2, [3, 4, [5, 6, [7, 8, [9, 10]]]]];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr4.flat(Infinity);<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 扁平化数组空项,如果原数组有空位,flat()方法会跳过空位<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />var arr4 = [1, 2, , 4, 5];<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />arr4.flat();<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// [1, 2, 4, 5]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  flatMap()方法对原数组的每个成员执行一个函数,相当于执行Array.prototype.map(),然后对返回值组成的数组执行flat()方法。

  该方法返回一个新数组,不改变原数组。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">// 相当于 [[2, 4], [3, 6], [4, 8]].flat()<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />[2, 3, 4].flatMap((x) => [x, x * 2])<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// [2, 4, 3, 6, 4, 8]<br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  22. entries(),keys() 和 values() 【ES6】

  entries(),keys()和values() —— 用于遍历数组。它们都返回一个遍历器对象,可以用for...of循环进行遍历

  区别是keys()是对键名的遍历、values()是对键值的遍历,entries()是对键值对的遍历

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;"><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />for (let index of ['a', 'b'].keys()) {  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(index);  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 0  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 1  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />for (let elem of ['a', 'b'].values()) {  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(elem);  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 'a'  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 'b'  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />for (let [index, elem] of ['a', 'b'].entries()) {  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(index, elem);  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />}  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 0 "a"  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />// 1 "b" <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  如果不使用for...of循环,可以手动调用遍历器对象的next方法,进行遍历。

  <pre data-tool="mdnice编辑器" style="margin-top: 10px;margin-bottom: 10px;max-width: 100%;color: rgb(0, 0, 0);font-size: 16px;text-align: left;background-color: rgb(255, 255, 255);border-radius: 5px;box-shadow: rgba(0, 0, 0, 0.55) 0px 2px 10px;box-sizing: border-box !important;word-wrap: break-word !important;">let letter = ['a', 'b', 'c'];  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />let entries = letter.entries();  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(entries.next().value); // [0, 'a']  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(entries.next().value); // [1, 'b']  <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" />console.log(entries.next().value); // [2, 'c'] <br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /><br style="max-width: 100%;box-sizing: border-box !important;word-wrap: break-word !important;" /></pre>

  参考资料

  blog.csdn.net/lizhiqiang1…

  <pre style="max-width: 100%;font-size: 16px;text-align: left;color: rgb(62, 62, 62);font-weight: 700;letter-spacing: 0.544px;widows: 1;word-spacing: 2px;background-color: rgb(255, 255, 255);box-sizing: border-box !important;word-wrap: break-word !important;"><p data-tool="mdnice编辑器" style="margin-top: 6px;margin-bottom: 6px;padding-top: 6px;padding-bottom: 6px;max-width: 100%;min-height: 1em;white-space: normal;letter-spacing: 2px;font-family: Optima-Regular, Optima, PingFangTC-Light, PingFangSC-light, PingFangTC-light;color: rgb(1, 1, 1);line-height: 26px;font-size: 13px;box-sizing: border-box !important;word-wrap: break-word !important;">如果觉得这篇文章还不错,来个【分享、点赞、在看】三连吧,让更多的人也看到~
</pre></p>
  源自:

文章由官网发布,如若转载,请注明出处:https://www.veimoz.com/1981
0 评论
403

发表评论

!