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 »

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

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

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 电子元器件分销10亿俱乐部[通俗易懂]

    电子元器件分销10亿俱乐部[通俗易懂]2015中国本土元器件分销商10亿俱乐部成员:1、科通集团简介:科通集团创建于1995年,是中国最大的IC元器件分销商。总部设于深圳,并在香港和上海有分公司。科通为国内的OEM厂商、ODM厂商和EMS厂商提供范围广泛的电子元器件,其应用涉及无线通信、电信设备、企业网络、数字媒体、家庭娱乐、汽车电子、工业控制等等众多领域。销售额:2015年营收-约84亿元员工人数:约400人产品线:Xili…

    2022年6月22日
    72
  • Java 版学生成绩管理系统,附源码[通俗易懂]

    Java 版学生成绩管理系统,附源码[通俗易懂]前言对于计算机专业的学生来讲,一定对于学生成绩管理系统课程设计十分印象深刻。设计、上机、编码、写报告、答辩等一系列流程,虽然很简单,但也可以综合运用我们所学的一些知识了。今天就来复习以下这个课题,用JavaSE来实现该课题,也算是补上当初上课的时候没有好好写的遗憾吧。虽然很简单,但是对于刚涉足编程学习的人来讲,还是有一定难度的,既要考虑界面打印,也要考虑条件判断、循环语句、输入输出控制等等技巧,所以在这里简单地实现一下,方便给初学的小伙伴们一个参考(对于我的界面比较丑的问题,就不要过于纠结了,下

    2022年7月13日
    22
  • Tomcat部署war包项目请求404「建议收藏」

    Tomcat部署war包项目请求404「建议收藏」Tomcat部署war包请求404

    2022年6月5日
    364
  • HTTP协议的基础

    HTTP协议的基础HTTP协议HTTP协议一、网络基础1、TCP/IP协议族各层作用应用层传输层网络层链路层2、TCP/IP通信传输流3、关系密切的IP、TCP、DNS协议(3次握手)4、URI和URL二、简单的HTTP协议1、通过请求和响应的交换达成通信2、HTTP是一种无状态协议3、HTTP方法4、持久连接keep-alive5、cookie三、HTTP报文2、用于HTTP协议交互的信息称为HTTP报文2、…

    2022年6月24日
    35
  • 管道(Pipe)/createPipe

    管道(Pipe)/createPipe

    2021年12月9日
    42
  • lnk2019无法解析的外部符号_declspec_error lnk1120无法解析的外部命令

    lnk2019无法解析的外部符号_declspec_error lnk1120无法解析的外部命令1.前言errorLNK2019:无法解析的外部符号这个错之前见过很多次,能知道最根本的原因在于链接过程中没有搜索到程序用到的库文件,即*.lib。笔记本重装了系统,有32Bit升到64Bit,运行VTK程序时,始终报错如下:1>  正在创建库E:\Driverprogram\imgport\Debug\imgport.lib和对象E:\Driverprog

    2022年10月6日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

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