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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 磁盘阵列的组建及维护技巧(图)

    磁盘阵列的组建及维护技巧(图)

    2021年8月9日
    64
  • Swift语言精要 – Dictionary(字典)

    Swift语言精要 – Dictionary(字典)

    2021年6月20日
    104
  • js 刷新当前页面

    js 刷新当前页面方法1:reload()方法reload()方法用于刷新当前文档。reload()方法类似于你浏览器上的刷新页面按钮。location.reload();方法2:replace()方法replace()方法可用一个新文档取代当前文档。<!DOCTYPEhtml><html><head><metacharset=”utf-8″><title>菜鸟教程(runoob.com)</title><sc

    2022年7月27日
    6
  • 51单片机毕业设计题目_51单片机经典项目

    51单片机毕业设计题目_51单片机经典项目STC12C5A60S2DS12887单片机毕业设计51单片机项目数码管显示的合成出租车计价器设计Synthetictaximeterdigitaldisplaydesign学生姓名: 学生学号: 10700121专业名称: 电子信息工程指导教师:计算机与信息工程学院2014年6月13日独创性声明本人声明所呈交的毕业设计(论文)是本人在指导教师指导下进行的研究工作和取得的研究成果,除了文中特别加以引用标注之处外,论文中不包含其他人已经发表或撰写过的研究成果,没有伪造数据的

    2022年10月3日
    3
  • torch.nn.Conv1d及一维卷积详解[通俗易懂]

    近日在搞wavenet,期间遇到了一维卷积,在这里对一维卷积以及其pytorch中的API进行总结,方便下次使用之前对二维卷积是比较熟悉的,在初次接触一维卷积的时候,我以为是一个一维的卷积核在一条线上做卷积,但是这种理解是错的,一维卷积不代表卷积核只有一维,也不代表被卷积的feature也是一维。一维的意思是说卷积的方向是一维的。下边首先看一个简单的一维卷积的例子(batchsize是1,也只…

    2022年4月13日
    495
  • 用C++Builder 6编写VCL控件

    用C++Builder 6编写VCL控件用C++Builder6编写VCL控件warton2002.12西安  如今天使用Borland的开发工具的程序员越来越多了,许多程序员开始从VC++,BC++等工具转向C++Builder(以下简称BCB 即BorlandC++Builder)和Delphi。要使用这两种开发工具,你就要用到VCL(VisualComponentLibrary可视化控件库)。关

    2022年9月16日
    2

发表回复

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

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