javascript
JavaScript中的数组
Here we are discussing one of the most useful data structure, Array.
在這里,我們討論最有用的數(shù)據(jù)結(jié)構(gòu)之一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.
按照常規(guī)的數(shù)組定義 , “數(shù)組是數(shù)據(jù)類型的同類集合。 但是在JS中 ,數(shù)組只是不同數(shù)據(jù)類型的集合,可能相同也可能不同。
Now let’s see an example:
現(xiàn)在讓我們看一個(gè)例子:
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 3week.pop(); //line 4 week.pop();console.log(week); //line 5week.push('Saturday'); // week.push(1) //Line 6console.log('After Line 6\n',week);week.unshift('Sunday'); //Line 7 console.log('After Line 7\n',week);week.splice(3,1,"IncludeHelp"); //line 8console.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.
在上面的代碼片段中,我們創(chuàng)建了一個(gè)名稱為week的數(shù)組,并創(chuàng)建了7個(gè)元素。 在第1行和第2行中,我們使用console.log打印數(shù)組周的元素。
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]
我們可以通過(guò)使用[]來(lái)訪問(wèn)數(shù)組的特定元素,在數(shù)組的名稱后面加上[],然后在方括號(hào)內(nèi)編寫元素的索引。 像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.
有趣的是,數(shù)組的索引從0開(kāi)始,而不是1,這意味著我們周數(shù)組中的Monday的索引將是0,而不是1,然后最后一個(gè)元素將成為6。因此,第1和2行將獲得第一個(gè)和數(shù)組的最后一個(gè)元素。
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.
現(xiàn)在移至第3行, 如其名稱所示, week.length返回?cái)?shù)組的長(zhǎng)度,在本例中為7。我們可以將其用作arrayName.length ,它將是一個(gè)整數(shù)值。
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函數(shù)每次被調(diào)用時(shí)都會(huì)從數(shù)組中刪除最后一個(gè)元素 ,其語(yǔ)法為arrayName.pop() 。 因此,在第5行中,連續(xù)2次彈出后得到5個(gè)元素。 同樣,shift函數(shù)將從頭刪除元素,其語(yǔ)法為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函數(shù)用于在數(shù)組的末尾添加元素 ,其語(yǔ)法為arrayName.push(element) ,其中元素是獨(dú)立于數(shù)據(jù)類型的,它可以是整數(shù)浮點(diǎn)值或字符串。 嘗試取消注釋第6行,并觀察會(huì)發(fā)生什么。 但是,如果我們想在開(kāi)始時(shí)添加一個(gè)元素,我們將使用unshift函數(shù),其語(yǔ)法類似于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:
現(xiàn)在,轉(zhuǎn)到拼接功能,該功能基本上是從一個(gè)位置刪除一個(gè)元素或一系列元素,并可以選擇在該位置添加/插入元素 。 讓我們看看它的語(yǔ)法arrayName.splice(startIndex,count,[可選]字符串) ,這里的起始索引是第一個(gè)元素將要?jiǎng)h除的索引,而count則指示要?jiǎng)h除的元素?cái)?shù),如果是,則僅刪除startIndex處的元素將被刪除,并說(shuō)count為2,則startIndex及其旁邊的元素將被刪除。 如果沒(méi)有傳遞第三個(gè)參數(shù),那么它們將被刪除,但其中包含一些元素,它們將在該索引處插入.splice可通過(guò)以下方式用于在特定索引處添加元素:
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
讀取方式: 數(shù)組的forEach方法
翻譯自: https://www.includehelp.com/code-snippets/arrays-in-javascript.aspx
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的JavaScript中的数组的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: “金珰易羁络”下一句是什么
- 下一篇: 调试JavaScript代码