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