前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >JavaScript Array Object

JavaScript Array Object

作者头像
全栈程序员站长
发布2022-09-14 10:21:07
2300
发布2022-09-14 10:21:07
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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 integermyCars[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 »

<script type=”text/javascript”> var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];document.write(“Removed: ” + fruits.splice(2,0,”Lemon”) + “<br />”);document.write(fruits); </script>

Removed:Banana,Orange,Lemon,Apple,Mango

<script type=”text/javascript”> var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];document.write(“Removed: ” + fruits.splice(2,0,”Lemon”) + “<br />”);document.write(fruits); </script>

Removed:Banana,Orange,Lemon,Apple,Mango

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: AppleBanana,Orange,Lemon,Mango Try it yourself »

<script type=”text/javascript”> var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];document.write(“Removed: ” + fruits.splice(2,1,”Lemon”) + “<br />”);document.write(fruits); </script>

Removed: AppleBanana,Orange,Lemon,Mango

<script type=”text/javascript”> var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];document.write(“Removed: ” + fruits.splice(2,1,”Lemon”) + “<br />”);document.write(fruits); </script>

Removed: AppleBanana,Orange,Lemon,Mango

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,MangoBanana,Orange,Lemon Try it yourself »

<script type=”text/javascript”> var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];document.write(“Removed: ” + fruits.splice(2,2,”Lemon”) + “<br />”);document.write(fruits); </script>

Removed: Apple,MangoBanana,Orange,Lemon

<script type=”text/javascript”> var fruits = [“Banana”, “Orange”, “Apple”, “Mango”];document.write(“Removed: ” + fruits.splice(2,2,”Lemon”) + “<br />”);document.write(fruits); </script>

Removed: Apple,MangoBanana,Orange,Lemon

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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年7月1,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Complete Array Object Reference
  • What is an Array?
  • Create an Array
  • Access an Array
  • Modify Values in an Array
  • Array Object Properties
  • Array Object Methods
  • splice() Method
    • Syntax
      • Browser Support
        • Examples
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档