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


相关推荐

  • w3c css规范_jquery是w3c标准吗

    w3c css规范_jquery是w3c标准吗2014年10月7日,W3CCEOJeffJaffe在W3C官方博客发表文章,讨论在Web技术标准制定过程中应当如何达成一致认同——是通过正式的认同流程(formalconsensusprocess)还是由标准编辑(informededitor)非正式的收集意见而达成认同。一致认同一直是W3C的核心价值,但WHATWG正在实行另外一种达成认同的方式,技术标准的编辑们通过他们的判断,决定…

    2025年12月12日
    5
  • 惠普服务器磁盘阵列配置及故障修复「建议收藏」

    惠普服务器磁盘阵列配置及故障修复「建议收藏」设备上电开机,按键盘F10.稍等片刻进入开机界面》》》》》》惠普服务器磁盘阵列配置Raid1/Raid0惠普服务器磁盘阵列的设置是安装操作系统的先决条件。只有完成了磁盘阵列的设置才能正常使用。HPDL388Gen9系列,配两块HDD硬盘。步骤如下:https://jingyan.baidu.com/article/e3c78…

    2022年5月29日
    54
  • 服务器加网站防盗链,网站防盗链的设置方法介绍(适用于IIS和Apache)[通俗易懂]

    服务器加网站防盗链,网站防盗链的设置方法介绍(适用于IIS和Apache)[通俗易懂]这篇文章主要为大家详细介绍了网站防盗链的设置方法介绍(适用于IIS和Apache),具有一定的参考价值,感兴趣的小伙伴们可以参考一下,有需要的朋友可以收藏方便以后借鉴。做网站的朋友一般都会遇到这样的一种情况,就是别人的网站经常会调用我们自己网站的图片或者文件,这无形之中会增加我们的服务器的压力,尤其是对于一些服务器带宽并不是十分富裕的网站来说就更是雪上加霜。因此我们需要学会设置防盗链来应对或者说来…

    2022年7月23日
    26
  • windows下使用labelImg标注图像

    windows下使用labelImg标注图像工作中遇到一个更好用的标注软件,速度快,操作方便,分享一下:https://blog.csdn.net/python_pycharm/article/details/102685591用于深度网络训练的数据集做标注的方法和工具有好多,像Labelme、labelImg、yolo_mark、Vatic、Sloth等等,此处暂时只介绍其中的一种标注工具:labelImg。等到后期…

    2022年6月20日
    50
  • 4个线程池_vc2010线程win32线程已退出

    4个线程池_vc2010线程win32线程已退出在windows中,系统提供了QueueUserWorkItem函数实现异步调用,这个函数相当于在线程池中建立多个用户工作项目,跟普通线程机制一样,线程池也有线程的同步等机制。 【函数原型】BOOLWINAPIQueueUserWorkItem(__inLPTHREAD_START_ROUTINEFunction,__inP…

    2022年9月24日
    3
  • Kong插件开发向导

    Kong插件开发向导转载李亚飞大佬的文章:https://www.lyafei.com/简介前面洋洋洒洒写了那么多文章,Kong搭建、Konga搭建、Kong插件开发工具包、Lua算法实现等等,就为了这篇Kong插件开发铺垫,在进一步讨论之前,有必要再简要阐述下Kong是如何构建的,特别是它如何与Nginx集成,以及它与Lua脚本之间的关系。使用lua-nginx-module模块可以在Nginx中启用Lua脚本功能,Kong与OpenResty一起发布,OpenResty中已经包.

    2022年6月26日
    45

发表回复

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

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