Rumah  >  Artikel  >  hujung hadapan web  >  JavaScript中数组函数array()的相关介绍

JavaScript中数组函数array()的相关介绍

巴扎黑
巴扎黑asal
2017-08-17 14:49:061504semak imbas

在程序语言中数组的重要性不言而喻,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>

Atas ialah kandungan terperinci JavaScript中数组函数array()的相关介绍. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn