Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of element Operation in JavaScript Array

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the JavaScript array element operation method related knowledge, the content is detailed and easy to understand, the operation is simple and fast, has a certain reference value, I believe that everyone after reading this JavaScript array element operation method article will have a harvest, let's take a look at it.

Access the first array element

Example

Fruits = ["Banana", "Orange", "Apple", "Mango"]

Var first = fruits [0]

Access the last array element

Example

Fruits = ["Banana", "Orange", "Apple", "Mango"]

Var last = fruits [fruits.length-1]

Traversing array elements

The safest way to traverse an array is to use the "for" loop:

Example

Var fruits, text, fLen, i

Fruits = ["Banana", "Orange", "Apple", "Mango"]

FLen = fruits.length

Text = ""

For (I = 0; I < fLen; iTunes +) {

Text + = "" + fruits [I] + ""

}

You can also use the Array.foreach () function:

Example

Var fruits, text

Fruits = ["Banana", "Orange", "Apple", "Mango"]

Text = ""

Fruits.forEach (myFunction)

Text + = ""

Function myFunction (value) {

Text + = "" + value + ""

}

Add array elements

The best way to add new elements to an array is to use the push () method:

Example

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Fruits.push ("Lemon"); / / add a new element (Lemon) to fruits

You can also use the length attribute to add new elements to the array:

Example

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Fruits [fruits.length] = "Lemon"; / / add a new element (Lemon) to fruits

Warning!

Add elements with the highest index to create undefined "holes" in the array:

Example

Var fruits = ["Banana", "Orange", "Apple", "Mango"]

Fruits [6] = "Lemon"; / / add a new element (Lemon) to fruits

This is the end of the article on "methods for manipulating JavaScript array elements". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to operate JavaScript array elements". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report