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


相关推荐

  • 传统线程技术(一)

    传统线程技术(一)

    2022年1月25日
    39
  • java激活码(JetBrains全家桶)

    (java激活码)本文适用于JetBrains家族所有ide,包括IntelliJidea,phpstorm,webstorm,pycharm,datagrip等。IntelliJ2021最新激活注册码,破解教程可免费永久激活,亲测有效,下面是详细链接哦~https://javaforall.net/100143.html…

    2022年3月21日
    119
  • signal 聊天记录_实时查看车辆位置app

    signal 聊天记录_实时查看车辆位置app开发工具与关键技术:js作者:GuanLW撰写时间:2022/3/18第一步:先在vs的NuGet包中下载安装Microsoft.AspNet.SignalR。第二步:右键项目->新建项,选择signalr集线器类,并写入代码,namespace与类名记得改成实际类名如下:usingMicrosoft.AspNet.SignalR;usingMicrosoft.AspNet.SignalR.Hubs;usingSystem;usingSystem.C

    2022年10月19日
    1
  • 最新版微信小程序如何引入iconfont阿里矢量图库解决方案

    最新版微信小程序如何引入iconfont阿里矢量图库解决方案前言:问题引发的原因来自最近在写微信小程序教学项目,项目中的一个图片是我随手切的,因为之前在Vue项目中一直在使用阿里矢量图库,我就想把阿里矢量图库ico图标集成到自己项目中,百度看了一些博客跟着做都引入不成功,研究了二十分钟弄出来,特此记录一下,同时作为参考文档供教学使用。1、首先我们打开阿里矢量官网接着我们需要登录一下,如果没有账号这里可以使用微博注册登录一下,登录成功后,点击【资源管理–>>我的项目】接着我们点击【新建项目】填写【项目名称:这里随便…

    2025年6月10日
    3
  • py2exe用法_py import

    py2exe用法_py import使用pyinstaller,真是受够了,各种bug,各种莫名其妙的情况,也是够了使用py2exe,学习的时候麻烦,但是打包时候真的太方便了安装py2exe,网址http://www.py2exe.org/选择对应的版本下载;撰写setup.py文件`#–coding:utf-8–importpy2exefromdistutils.coreimportsetupsetu

    2022年9月10日
    3
  • 表结法和账结法_我国采用表结法还是账结法

    表结法和账结法_我国采用表结法还是账结法为了计算出当期利润,通常采用两种方法:表结法和账结法。国内一些财务系统多采用账结法,这似乎比较符合中国式习惯。即:期末,损益类的虚账户的余额结转汇总到本年利润科目,最后账户无余额;资产负债类实账户结

    2022年8月4日
    6

发表回复

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

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