Here we are discussing one of the most useful data structure, Array.
在这里,我们讨论最有用的数据结构之一Array 。
By conventional definition of arrays, "Arrays are the homogeneous collection of data types. But in JS, Arrays simply are the collection of different data types, may or may not be the same.
按照常规的数组定义 , “数组是数据类型的同类集合。 但是在JS中 ,数组只是不同数据类型的集合,可能相同也可能不同。
Now let’s see an example:
现在让我们看一个例子:
let week = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturdday','Sunday'];
console.log(week[0]); //line 1
console.log(week[6]); //line 2
console.log('Size of array',week.length); //line 3
week.pop(); //line 4
week.pop();
console.log(week); //line 5
week.push('Saturday');
// week.push(1) //Line 6
console.log('After Line 6\n',week);
week.unshift('Sunday'); //Line 7
console.log('After Line 7\n',week);
week.splice(3,1,"IncludeHelp"); //line 8
console.log('After line 8\n',week);
In the above code snippet, we created an array of name week, we created with 7 elements. In line 1 and 2 we used console.log to print the element of our array week.
在上面的代码片段中,我们创建了一个名称为week的数组,并创建了7个元素。 在第1行和第2行中,我们使用console.log打印数组周的元素。
We can access to a particular element of an array by using [ ] proceeded by name of the array and inside the square brackets we write the index of the element. Like arrayName[Index]
我们可以通过使用[]来访问数组的特定元素,在数组的名称后面加上[],然后在方括号内编写元素的索引。 像arrayName [Index]
The interesting thing is indexing of the array starts with 0, not 1, that means the index of Monday in our week array will be 0, not 1 and then the last element will become 6. Thus line 1 & 2 we will get a first and last element of the array respectively.
有趣的是,数组的索引从0开始,而不是1,这意味着我们周数组中的Monday的索引将是0,而不是1,然后最后一个元素将成为6。因此,第1和2行将获得第一个和数组的最后一个元素。
Now move to line 3, week.length as the name suggests it return the length of the array, in our case, it’s 7. We can use it as arrayName.length and it will be an integer value.
现在移至第3行, 如其名称所示, week.length返回数组的长度,在本例中为7。我们可以将其用作arrayName.length ,它将是一个整数值。
The pop function will delete the last element from the array every time being called, the syntax for it is arrayName.pop(). Therefore, in line 5, we got 5 elements after 2 successive pops. Similarly, shift function will delete an element from the beginning and its syntax is arrayName.shift().
pop函数每次被调用时都会从数组中删除最后一个元素 ,其语法为arrayName.pop() 。 因此,在第5行中,连续2次弹出后得到5个元素。 同样,shift函数将从头删除元素,其语法为arrayName.shift() 。
The push function is used to add an element at the end of the array, and its syntax is arrayName.push(element), where the element is data type independent it could be integer floating value or a string. Try uncommenting line 6 and observe what happens. But if we wanted to add an element at the beginning we shall use unshift function which is having a syntax similar to pop as arrayName.unshift(element) like we used in line 7.
push函数用于在数组的末尾添加元素 ,其语法为arrayName.push(element) ,其中元素是独立于数据类型的,它可以是整数浮点值或字符串。 尝试取消注释第6行,并观察会发生什么。 但是,如果我们想在开始时添加一个元素,我们将使用unshift函数,其语法类似于pop作为arrayName.unshift(element),就像在第7行中使用的那样。
Now, move to splice function which basically, removes an element or series of elements from a position and optionally can add/insert an element at that position. Let’s see its syntax, arrayName.splice(startIndex, count,[optional] string), here the start index is the index from where the first element will delete and the count tells the number of elements to delete, if it’s one then only the element at startIndex will be delete and say count is two then the startIndex and element next to it will be deleted. If no third argument passed so they will be deleted but it contains some element they will insert at that index.splice can be used to add an element at a particular index with the following way:
现在,转到拼接功能,该功能基本上是从一个位置删除一个元素或一系列元素,并可以选择在该位置添加/插入元素 。 让我们看看它的语法arrayName.splice(startIndex,count,[可选]字符串) ,这里的起始索引是第一个元素将要删除的索引,而count则指示要删除的元素数,如果是,则仅删除startIndex处的元素将被删除,并说count为2,则startIndex及其旁边的元素将被删除。 如果没有传递第三个参数,那么它们将被删除,但其中包含一些元素,它们将在该索引处插入.splice可通过以下方式用于在特定索引处添加元素:
arrayName.splice(Index,0, element) this will add the element at the mentioned index without deleting the prior elements, their position will be increment by one.
arrayName.splice(Index,0,element)这将在提到的索引处添加元素,而不删除先前的元素,它们的位置将增加一。
Output for the above code:
上面代码的输出:

Read mode: forEach method of array
读取方式: 数组的forEach方法
翻译自: https://www.includehelp.com/code-snippets/arrays-in-javascript.aspx