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)


相关推荐

  • WOFF格式「建议收藏」

    WOFF格式「建议收藏」WOFF格式WOFF文件格式是用WOFF(WebOpenFontFormat)创建的网页开放字体格式,Web开放字体格式(WebOpenFontFormat,简称WOFF)是一种网页所采用的字体格式标准,保存一个压缩的容器中,支持TTF(TrueType)字体和OPT(OpenType)字体,转载于:https://www.cnblogs.com/xiatc/p/8944385…

    2022年10月30日
  • 笔记本没有声音,f1一直亮_为什么笔记本按f1不静音

    笔记本没有声音,f1一直亮_为什么笔记本按f1不静音某个平凡的晚上,博主正准备听会儿音乐却发现怎么也打不开声音,通知键盘左上的F1健一直亮着。几经搜寻终找到如下解决方案:win+r出对话框,然后输出services.msc后会出现如下的界面,然后找到LenovoHotkeyClientLoader,点进去选择重启然后再按F1灯就灭了,还能外放了以上只是经过本人试验的有效方法之一,大家可在遇到类似情况时试一试,有其他更好方法的朋友欢迎在评论区补充。…

  • 关于COM对象创建(CoCreateInstance,与QueryInterface)

    关于COM对象创建(CoCreateInstance,与QueryInterface)panda2002-12-1905:40PM一个土问题,关于COM对象的创建 因为生活所迫,这几天必须对COM有所了解。今天看了之后,对COM对象的创建有所困惑。使用CoCreateInstance和QueryInterface两种方法创建COM对象有何不同?例子程序如下:代码://CreateaCOMobjectfromtheShellLinkcoclasshr=CoCr

  • 特征选择/筛选方法总结

    作者:jlianghttps://blog.csdn.net/jliang31.特征选择介绍1)特征分类相关特征:对于学习任务(例如分类问题)有帮助,可以提升学习算法的效果; 无关特征:对于我们的算法没有任何帮助,不会给算法的效果带来任何提升; 冗余特征:不会对我们的算法带来新的信息,或者这种特征的信息可以由其他的特征推断出;2)特征选择的目的对于一个特定的学习算法…

  • 微信小程序一个js文件读取多个接口数据「建议收藏」

    微信小程序一个js文件读取多个接口数据「建议收藏」我也不知道这个叫啥问题我也不知道这样写是不是不对数据确实拿到了data:{warningarr:null,mydata:null,mySensor:null},/***生命周期函数–监听页面加载*/onLoad:function(options){ ……/*自己的…

  • 计算两个矩阵之间的欧式距离「建议收藏」

    计算两个矩阵之间的欧式距离「建议收藏」在我们使用k-NN模型时,需要计算测试集中每一点到训练集中每一点的欧氏距离,即需要求得两矩阵之间的欧氏距离。在实现k-NN算法时通常有三种方案,分别是使用两层循环,使用一层循环和不使用循环。使用两层循环分别对训练集和测试集中的数据进行循环遍历,计算每两个点之间的欧式距离,然后赋值给dist矩阵。此算法没有经过任何优化。num_test=X.shape[0]num_…

发表回复

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

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