JavaScript Array Object

JavaScript Array ObjectCreateanarrayCreateanarray,assignvaluestoit,andwritethevaluestotheoutput.(Youcanfindmoreexamplesatthebottomofthispa

大家好,又见面了,我是你们的朋友全栈君。

Create an array
Create an array, assign values to it, and write the values to the output.

(You can find more examples at the bottom of this page)


Complete Array Object Reference

For a complete reference of all the properties and methods that can be used with the Array object, go to our complete Array object reference.

The reference contains a brief description and examples of use for each property and method!


What is an Array?

An array is a special variable, which can hold more than one value, at a time.

If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this:

var car1=”Saab”;
var car2=”Volvo”;
var car3=”BMW”;

However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?

The best solution here is to use an array!

An array can hold all your variable values under a single name. And you can access the values by referring to the array name.

Each element in the array has its own ID so that it can be easily accessed.


Create an Array

An array can be defined in three ways.

The following code creates an Array object called myCars:

1:

var myCars=new Array(); // regular array (add an optional integer
myCars[0]=”Saab”;       // argument to control array’s size)
myCars[1]=”Volvo”;
myCars[2]=”BMW”;

2:

var myCars=new Array(“Saab”,”Volvo”,”BMW”); // condensed array

3:

var myCars=[“Saab”,”Volvo”,”BMW”]; // literal array

Note: If you specify numbers or true/false values inside the array then the variable type will be Number or Boolean, instead of String.


Access an Array

You can refer to a particular element in an array by referring to the name of the array and the index number. The index number starts at 0.

The following code line:

document.write(myCars[0]);

will result in the following output:

Saab


Modify Values in an Array

To modify a value in an existing array, just add a new value to the array with a specified index number:

myCars[0]=”Opel”;

Now, the following code line:

document.write(myCars[0]);

will result in the following output:

Opel

The Array object is used to store multiple values in a single variable.

For a tutorial about Arrays, read our JavaScript Array Object tutorial.


Array Object Properties

Property Description
constructor Returns the function that created the Array object’s prototype
length Sets or returns the number of elements in an array
prototype Allows you to add properties and methods to an object

Array Object Methods

Method Description
concat() Joins two or more arrays, and returns a copy of the joined arrays
indexOf()  
join() Joins all elements of an array into a string
pop() Removes the last element of an array, and returns that element
push() Adds new elements to the end of an array, and returns the new length
reverse() Reverses the order of the elements in an array
shift() Removes the first element of an array, and returns that element
slice() Selects a part of an array, and returns the new array
sort() Sorts the elements of an array
splice() Adds/Removes elements from an array
toString() Converts an array to a string, and returns the result
unshift() Adds new elements to the beginning of an array, and returns the new length
valueOf() Returns the primitive value of an array

splice() Method

Definition and Usage


The splice() method adds and/or removes elements to/from an array, and returns the removed element(s).

Note: This method changes the original array!

Syntax

array.splice(index,howmany,element1,…..,elementX)

Parameter Description
index Required. An integer that specifies at what position to add/remove elements
howmany Required. The number of elements to be removed. If set to 0, no elements will be removed
element1, …, elementX Optional. The new element(s) to be added to the array


Browser Support

The splice() method is supported in all major browsers.


Examples

Example 1

Add an element to position 2 in the array:

<script type=”text/javascript”>

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
document.write(“Removed: ” + fruits.splice(2,0,”Lemon”) + “<br />”);
document.write(fruits);

</script>

The output of the code above will be:

Removed:
Banana,Orange,Lemon,Apple,Mango

Try it yourself »

Example 2

Remove one element from position 2, and add a new element to position 2 in the array:

<script type=”text/javascript”>

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
document.write(“Removed: ” + fruits.splice(2,1,”Lemon”) + “<br />”);
document.write(fruits);

</script>

The output of the code above will be:

Removed: Apple
Banana,Orange,Lemon,Mango

Try it yourself »

Example 3

Remove two elements, start from position 2, and add a new element to position 2 in the array:

<script type=”text/javascript”>

var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];
document.write(“Removed: ” + fruits.splice(2,2,”Lemon”) + “<br />”);
document.write(fruits);

</script>

The output of the code above will be:

Removed: Apple,Mango
Banana,Orange,Lemon

Try it yourself »

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/158418.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 华为OJ机试训练(一)

    华为OJ机试训练(一)

  • js编写HTML通过document.cookie写入不了cookie的问题[通俗易懂]

    js编写HTML通过document.cookie写入不了cookie的问题[通俗易懂]js中通过document.cookie写入不了cookie的问题使用VScode编写HTML应用cookie进行存储,发现编写无法读取到cookie的内容,即未能实现cookie存储。作为新手问题,可以考虑这个原因:只有当用在服务器或者本地的服务器中的时候,才能使用这个方法写入cookie,所以VScode没有使用服务器方式?这个原因可以通过方案一尝试解决:打开VScode,点击左侧扩展,输入liveserver,点击安装即可;安装成功后再VS界面右下角可以看到相应提示

  • 高德地图的标志放大_高德地图点标注的分布与缩放

    高德地图的标志放大_高德地图点标注的分布与缩放本文介绍了在ReactNative平板开发中使用高德地图组件的一个案例,重点介绍了如何计算并缩放到所需状态,以及地图Webview与ReactNative通信的方式。欢迎关注我的专栏:熵与单子的代码本。在数据可视化展示系统中,地理信息系统(GIS)组件已经几乎是必备的了。GIS组件通过地图的形式直观地展现数据项在地理上的分部,以地图上点标注(Marker)的颜色、大小…

  • Python Selenium库的使用「建议收藏」

    (一)Selenium基础入门教程:Selenium官网教程1.Selenium简介Selenium是一个用于测试网站的自动化测试工具,支持各种浏览器包括Chrome、Firefox、Safari等主流界面浏览器,同时也支持phantomJS无界面浏览器。2.支持多种操作系统如Windows、Linux、IOS、Android等。3.安装Selenium…

  • navicat 15 激活码 mac【中文破解版】[通俗易懂]

    (navicat 15 激活码 mac)这是一篇idea技术相关文章,由全栈君为大家提供,主要知识点是关于2021JetBrains全家桶永久激活码的内容IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.cn/100143.html1STL5S9V8F-eyJsaWN…

  • tcpdf的方法_tcp fin

    tcpdf的方法_tcp fin$pdf=newTCPDF(PDF_PAGE_ORIENTATION,PDF_UNIT,PDF_PAGE_FORMAT,true,’UTF-8′,false);页面记得也设为utf-8AddPage();新建一个pdf文档页面。Image($file,$x,$y,$w,$h,$type,$link,$align,$resize,$dpi,$p

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号