JavaScript中的數組

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:

上面代碼的輸出:

arrays output in JS

Read mode: forEach method of array

讀取方式: 數組的forEach方法

翻譯自: https://www.includehelp.com/code-snippets/arrays-in-javascript.aspx

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/377429.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377429.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377429.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

【C++基礎】異常處理機制概要

目錄C的異常處理方法知識要點踹扔抓的代碼塊示例兩種處理被0除的方法異常處理機制的優點其他語言中的異常處理C函數異常聲明C的異常處理方法知識要點 理解“踹扔抓”三部曲的結構,尤其是catch是怎么匹配異常的。 知道C標準庫中的異常類都是從exception繼承下來的&am…

[轉載]Struts2 獲得Session和Request

轉自http://www.blogjava.net/lyyb2001/archive/2008/03/07/184593.html 在struts1中,獲得到系統的request或者session對象非常方便,都是按照形參傳遞的,但是在struts2中,request和session都被隱藏了struts提供兩種方式訪問sessio…

WPS根據章節編號依次排序

第Ⅲ章節有四小部分,分別為A、B、C、D 第Ⅳ章節要重新開始編號,從A開始 操作步驟: ①再D后面回車(紅線位置回車),生成E ②把Ⅳ中待寫內容寫到E中 ③將E復制到Ⅳ下,這里需要注意D的換行也要復制…

【C++基礎】異常匹配與內建異常類

目錄異常匹配catch: 按異常類型匹配為何要使用異常類內建異常類標準庫中的異常基類標準庫中的異常類例1:vector下標訪問越界out_of_range異常例2:內存分配失敗bad_alloc異常例3:側向轉換失敗bad_cast異常類幾種情況,使用對應異常異…

scala 訪問修飾符_Scala中的訪問修飾符

scala 訪問修飾符Access modifiers are used in order to restrict the usage of a member function to a class or a package. Using access modifiers data hiding takes place which is a very important concept of OOPs. 訪問修飾符用于將成員函數的使用限制為類或包。 使…

小試---EF5.0入門實例1

現在做個小練習吧~~~ 第一步:首先新建一個數據庫名字為Test;數據庫里面只有一個表UserTable 腳本為: USE [master] GO /****** 對象: Database [Test] 腳本日期: 12/15/2013 18:51:54 ******/ CREATE DATABASE [Test] ON PRIMARY ( NAME NTest, F…

iScroll4 禁止select等頁面元素默認事件的解決方法 轉

iScroll4 禁止select等頁面元素默認事件的解決方法起因在于onBeforeScrollStart : function(e){ e.preventDefault(); },這一行,iSroll禁止了事件的默認行為,導致select,option,textarea等元素無法點擊。解決方法也很簡單&#xf…

C++中比較兩個浮點數是否相等

來源&#xff1a; https://stackoverflow.com/a/37686/3242645 代碼&#xff1a; #include <cmath> #include <limits> bool AreSame(double a, double b) {return std::fabs(a - b) < std::numeric_limits<double>::epsilon(); }

MPEG的完整形式是什么?

MPEG&#xff1a;運動圖像專家組 (MPEG: Moving Picture Experts Group) MPEG is an abbreviation of Moving Picture Experts Group. It is a working group of authorities that is founded to establish standards for audio and video compression and transmission. The a…

正則 去除html標記

//string regexstr "<[^>]*>"; //去除所有的標簽 //"<script[^>]*?>.*?</script>" //去除所有腳本&#xff0c;中間部分也刪除 // string regexstr "<img[^>]*>"; //去除圖片的正則 // string regexstr &…

自畫PopMenu彈出

BorderColor:TColor; //邊框顏色FillColor:TColor; //未選中填充顏色TextColor:TColor; //未選中字體顏色SelectTextColor:TColor; //選中字體顏色SelectFillColor:TColor; //選中填充顏色SideBuffer:Integer; //邊框寬度procedure TForm1.FormCreate(Sender: TObject); b…

安利一款倒計時插件---雨滴桌面

內容來自B站(搜索Rainmeter即可)&#xff0c;里面教程很多&#xff0c;因為視頻看的有點麻煩&#xff0c;故進行了整理 一、下載安裝包、解壓、安裝 免費下載連接&#xff0c;不需要積分 skin文件夾存放皮膚的一些配置文件&#xff0c;因為原本皮膚太low了 第二個是可執行文…

【C++基礎】自定義異常類與多重捕獲

目錄自定義異常類構建過程例&#xff1a;Vec3D類的數組下標越界的異常類捕獲多種無關異常不同的異常的捕獲捕獲派生異常異常處理的次序例子&#xff1a;多重捕獲異常類catch塊的參數類型可以不用引用類型嗎?自定義異常類 自定義異常類通常由exception或其后代類派生。這樣我們…

gprs 睡眠模式_GPRS的完整形式是什么?

gprs 睡眠模式GPRS&#xff1a;通用分組無線業務 (GPRS: General Packet Radio Service) GPRS is an abbreviation of General Packet Radio Service. It is a non-voice, high-level speed packet switching technology planned for GSM networks. On 2G and 3G cellular tran…

int main(int argc,char* argv[])講解

分類&#xff1a; 學習筆記2011-11-07 21:502354人閱讀評論(0)收藏舉報dos編譯器pathunixcommandc在最近學習中老是遇到 int main(int argc,char* argv[])&#xff0c;以為就是簡單的參數應用了&#xff0c;但是看代碼是沒能理解參數的具體傳遞過程&#xff0c;上網…

Maven實戰(七)——常用Maven插件介紹(上)

我們都知道Maven本質上是一個插件框架&#xff0c;它的核心并不執行任何具體的構建任務&#xff0c;所有這些任務都交給插件來完成&#xff0c;例如編譯源代碼是由maven-compiler-plugin完成的。進一步說&#xff0c;每個任務對應了一個插件目標&#xff08;goal&#xff09;&a…

【設計模式之美】<Reading Notes>抽象類與接口

抽象類特性 1、抽象類不允許被實例化&#xff0c;只能被繼承。 2、抽象類可以包含屬性和方法。方法既可以包含代碼實現&#xff0c;也可以不包含代碼實現。不包含代碼實現的方法叫做抽象方法。 3、子類繼承抽象類&#xff0c;必須實現抽象類中的所有抽象方法。 接口特性 1、…

多線程之間共享數據的實現

1&#xff1a;如果每個線程執行的代碼相同&#xff0c;可以使用同一個Runnable對象&#xff0c;然后將共享的數據放在Runnable里面&#xff0c;來實現數據的共享。 例如買票系統... package com.cn.gbx;import java.util.Date; import java.util.Random; import java.util.Time…

AIX的完整形式是什么?

AIX&#xff1a;高級交互式主管 (AIX: Advanced Interactive Executive) AIX is an abbreviation of "Advanced Interactive Executive". AIX是“ Advanced Interactive Executive”的縮寫 。 It is a progression sequence of proprietary UNIX operating systems …

c#生成隨機字符串 用做批量申請賬號時的隨機密碼還是相當不錯的

//隨機字符串生成器的主要功能如下&#xff1a; //1、支持自定義字符串長度 //2、支持自定義是否包含數字 //3、支持自定義是否包含小寫字母 //4、支持自定義是否包含大寫字母 //5、支持自定義是否包含特殊符號 //6、支持自定義字符…