首頁 > web前端 > js教程 > JavaScript中數組函數array()的相關介紹

JavaScript中數組函數array()的相關介紹

巴扎黑
發布: 2017-08-17 14:49:06
原創
1537 人瀏覽過

在程式語言中陣列的重要性不言而喻,JavaScript中陣列也是最常使用的物件之一,陣列是值的有序集合,由於弱型別的原因,JavaScript中陣列十分靈活、強大,不像是Java等強型別高階語言陣列只能存放相同型別或其子型元素,JavaScript在同一個陣列中可以存放多種型別的元素,而且是長度也是可以動態調整的,可以隨著資料增加或減少自動對數組長度做更改。

Array()是一個用來建構陣列的內建建構器函數。陣列主要由以下三種建立方式:

<span class="pln">array </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Array</span><span class="pun">()</span><br/><span class="pln">array </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Array</span><span class="pun">([</span><span class="pln">size</span><span class="pun">])</span><span class="pln">array </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">Array</span><span class="pun">(</span><span class="pln">element0</span><span class="pun">,</span><span class="pln"> element1</span><span class="pun">,</span><span class="pln"> </span><span class="pun">...,</span><span class="pln"> elementN</span><span class="pun">)</span>
登入後複製

參數

參數 size 是期望的陣列元素個數。傳回的數組,length 欄位將被設為 size 的值。

參數 element ..., elementn 是參數列表。當使用這些參數來呼叫建構函式 Array() 時,新建立的陣列的元素就會被初始化為這些值。它的 length 欄位也會被設定為參數的個數。

傳回值

傳回新建立並被初始化了的陣列。

如果呼叫建構函式 Array() 時沒有使用參數,那麼傳回的陣列為空,length 欄位為 0。

當呼叫建構函式時只傳遞給它一個數字參數,該建構函式會傳回具有指定個數、元素為 undefined 的陣列。

當其他參數呼叫 Array() 時,此建構函式將以參數指定的值初始化陣列。

當把建構函式當作函式調用,不使用 new 運算子時,它的行為與使用 new 運算子呼叫它時的行為完全一樣。

Array 物件方法

Array 物件屬性

concat()方法:

合併陣列

[1,2]concat([3,4],[5,6]);//[1,2,3,4,5,6]
登入後複製


join()方法:

<span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><br/><span class="pln"> </span><span class="kwd">var</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">];</span><br/><span class="pln"> a</span><span class="pun">.</span><span class="pln">join</span><span class="pun">(</span><span class="str">""</span><span class="pun">);</span><span class="com">// =>"123"</span><span class="tag"></script></span>
登入後複製


#pop()方法:

移除陣列最後一個元素,並將其傳回

<span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><br/><span class="pln"> </span><span class="kwd">var</span><span class="pln"> fruits </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"apple"</span><span class="pun">,</span><span class="str">"banana"</span><span class="pun">,</span><span class="str">"pineapple"</span><span class="pun">];</span><br/><span class="pln"> fruits</span><span class="pun">.</span><span class="pln">pop</span><span class="pun">();</span><span class="com">// pineapple </span><br/><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">fruits</span><span class="pun">);</span><span class="com">//["apple","banana"]</span><span class="tag"></script></span>
登入後複製

shift()方法:

刪除並傳回陣列的第一個元素。

<span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><br/><span class="pln"> </span><span class="kwd">var</span><span class="pln"> a </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="lit">1</span><span class="pun">,</span><span class="lit">2</span><span class="pun">,</span><span class="lit">3</span><span class="pun">];</span><br/><span class="pln"> a</span><span class="pun">.</span><span class="pln">shift</span><span class="pun">();</span><span class="com">//1</span><br/><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">a</span><span class="pun">);</span><span class="com">//[2,3]</span><span class="tag"></script></span>
登入後複製

slice(start,end)

方法:截取陣列的某一部分,不會對原始陣列進行任何修改。

<span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><br/><span class="pln">  </span><span class="kwd">var</span><span class="pln"> num </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"a"</span><span class="pun">,</span><span class="str">"b"</span><span class="pun">,</span><span class="str">"c"</span><span class="pun">,</span><span class="str">"d"</span><span class="pun">,</span><span class="str">"e"</span><span class="pun">];</span><br/><span class="pln">  console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">num</span><span class="pun">.</span><span class="pln">slice</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="lit">4</span><span class="pun">));</span><span class="com">//["b","c","d"]</span><br/><span class="pln">  console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">num</span><span class="pun">);</span><span class="com">//["a","b","c","d","e"]</span><span class="tag"></script></span>
登入後複製


splice(start,delete_count,i1,i2…)

方法:刪除陣列元素的同時新增新的元素。 i1,i2為要插入的新元素

<span class="tag"><script</span><span class="pln"> </span><span class="atn">type</span><span class="pun">=</span><span class="atv">"text/javascript"</span><span class="tag">></span><br/><span class="pln"> </span><span class="kwd">var</span><span class="pln"> arr </span><span class="pun">=</span><span class="pln"> </span><span class="pun">[</span><span class="str">"js"</span><span class="pun">,</span><span class="str">"css"</span><span class="pun">,</span><span class="str">"html"</span><span class="pun">,</span><span class="str">"php"</span><span class="pun">,</span><span class="str">"c"</span><span class="pun">];</span><br/><span class="pln"> arr</span><span class="pun">.</span><span class="pln">splice</span><span class="pun">(</span><span class="lit">1</span><span class="pun">,</span><span class="pln"> </span><span class="lit">2</span><span class="pun">,</span><span class="str">"python"</span><span class="pun">,</span><span class="str">"jquery"</span><span class="pun">);</span><span class="com">//["css","html"]</span><br/><span class="pln"> console</span><span class="pun">.</span><span class="pln">log</span><span class="pun">(</span><span class="pln">arr</span><span class="pun">);</span><span class="com">//["js","python","jquery""php","c"]</span><span class="tag"></script</span>
登入後複製

以上是JavaScript中數組函數array()的相關介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板