📜  ES6 |大批

📅  最后修改于: 2022-05-13 01:56:54.161000             🧑  作者: Mango

ES6 |大批

数组是值的同质集合。它是用于存储不同元素的单个变量。当我们想要存储元素列表并通过单个变量访问它们时,通常会使用它。与大多数语言中数组是对多个变量的引用不同,ES6 数组是存储多个元素的单个变量。当我们有少量对象时,我们可以使用普通变量(v1,v2,v3,..),但如果我们要存储大量实例,使用普通变量管理它们变得困难。数组的想法是在一个变量中表示许多实例。
数组的特征:

  • 索引在数组中可用,因此可以使用数组索引随机访问元素。
  • 可以使用单个变量名称存储多个元素。
  • 使用循环遍历数组变得容易。
  • 排序变得容易,因为它可以通过在数组中编写更少的代码行来完成。
  • 数组值可以修改或更新,但不能删除。

初始化数组:初始化或声明数组很容易。下面的程序实现了数组的初始化。

  • 程序:
javascript
// Initializing while declaring
var JS = ["ES1", "ES2", "ES3", "ES4", "ES5", "ES6"];
 
// Initializing after declaring
JS[0] = "ES6";
JS[1] = "ES6";
JS[2] = "ES6";
JS[3] = "ES6";


javascript


javascript


javascript


访问数组元素:要访问任何特定的数组元素,我们需要知道该元素的索引号。数组索引号用于访问数组元素。数组的索引总是从 0 开始。

  • 程序:

javascript


  • 输出:
ES5
ES6

Array Object: Array 构造函数可以作为表示数组大小的数值传递,并且数组元素是逗号分隔的值。

  • 方案一:

javascript

                   
  • 输出:
ES6
2015
ES8
2017
ES10
2019
  • 方案二:

javascript

                                   
  • 输出:
0
0.6666666666666666
1.3333333333333333
2
2.6666666666666665
3.3333333333333335

数组方法: ES6 中引入了很多数组方法。

MethodsDescription
concat()This method is used to merge two or more arrays together.
every()This function checks whether all the elements of the array satisfy the given condition or not that is provided by a function passed to it as the argument.
filter()This method creates a new array with elements that follow or pass the given criteria and condition.
find()Filter elements through the function, return first/all values that make it return true.
forEach()This method used to iterate through its elements and manipulate them.
Array.from()This change all thing that are array-like or iterable into true array especially when working with DOM, so that you can use other array methods like reduce, map, filter and so on.
indexof()This method is used to find the index of the first occurrence of the search element provided as the argument to the method.
join()This method is used to join the elements of an array into a string.
lastIndexOf()Look for item starting from position pos, return the index or -1 if not found.
map()This method create new array by calling the provided function in every element.
Array.of()This create array from every arguments passed into it.
pop()Removes the last element from an array and returns that element.
push()Adds one or more elements to the end of an array and returns the new length of the array.
reduce()The reduce() method applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value – MDN
reverse()Reverses the order of the elements of an array — the first becomes the last, and the last becomes the first.
shift()This method removes the first element of the array thus reducing the size of the original array by 1.
slice()Creates a new array, copies elements from position start till end (not inclusive) into it.
some()This method check if at least one of array’s item passed the condition. If passed, it return ‘true’ otherwise ‘false’.
sort()This method used to sort the array in place in a given order according to the compare() function.
splice()This methods is used to modify the contents of an array by removing the existing elements.
unshift()This function increases the length of the existing array by the number of elements added to the array.

注意:在 JavaScript 中,数组使用编号索引,对象使用命名索引。