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

JavaScript methods for changing elements, deleting elements, and splicing arrays

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today Xiaobian to share with you JavaScript change elements, delete elements, stitching array method of the relevant knowledge points, detailed content, clear logic, I believe that most people still know too much about this knowledge, so share this article for your reference, I hope you can learn something after reading this article, let's take a look at it.

Displacement element

The displacement is the same as the pop-up, but deals with the first element instead of the last.

The shift () method deletes the first array element and "shifts" all other elements to a lower index.

Example

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

Fruits.shift (); / / remove the first element "Banana" from fruits

The shift () method returns the string that has been "displaced":

Example

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

Fruits.shift (); / / returns "Banana"

The unshift () method (at the beginning) adds a new element to the array and "reverses" the old element:

Example

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

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

The unshift () method returns the length of the new array.

Example

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

Fruits.unshift ("Lemon"); / / returns 5

Change element

Access array elements by using their index numbers:

The array index (subscript) starts with 0. [0] is the first array element, [1] is the second, and [2] is the third.

Example

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

Fruits [0] = "Kiwi"; / / change the first element of fruits to "Kiwi"

The length attribute provides an easy way to append new elements to an array:

Example

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

Fruits [fruits.length] = "Kiwi"; / / append "Kiwi" to fruits

Delete element

Since the JavaScript array belongs to an object, the elements can be deleted using the JavaScript delete operator:

Example

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

Delete fruits [0]; / / change the first element in fruits to undefined

Using delete leaves undefined holes in the array. Use pop () or shift () instead.

Splicing array

The splice () method can be used to add new items to an array:

Example

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

Fruits.splice (2,0, "Lemon", "Kiwi")

The first parameter (2) defines where the new element should be added (splicing).

The second parameter (0) defines how many elements should be deleted.

The remaining parameters ("Lemon", "Kiwi") define the new elements to be added.

The splice () method returns an array of deleted items:

Example

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

Fruits.splice (2, 2, "Lemon", "Kiwi")

Use splice () to delete an element

With clever parameter settings, you can use splice () to remove elements without leaving "holes" in the array:

Example

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

Fruits.splice (0,1); / / Delete the first element in the fruits

The first parameter (0) defines where the new element should be added (accessed).

The second parameter (1) defines that multiple elements should be deleted.

The remaining parameters are omitted. No new elements will be added.

These are all the contents of the article "JavaScript changes elements, deletes elements, and splices arrays". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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