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

How ES6 modifies an element in an array

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

Share

Shulou(Shulou.com)05/31 Report--

This article focuses on "how ES6 modifies an element in an array". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how ES6 modifies an element in an array.

Modify method: 1, use the "array name [specify subscript value] = new value;" syntax, access the specified element through the subscript, and re-assign the value to the element to modify; 2, use the "array.splice (start the article, delete the number of elements, insert value 1 insert value 2)" statement, delete n elements from the specified position, and insert n new elements.

The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.

ES6 modifies an element in the array

Method 1: access the specified element through the subscript and reassign the value

The array is compound data, and the array name is a referential variable that points to the array, so the array is a referential object.

To access an array is to access array elements. Elements are equivalent to variables, and the elements of an array are a set of variables arranged in order. They have no identifiers, are indexed by subscript, and the subscript starts at 0 and increments sequentially. Note that the array subscript is a non-negative integer expression, or a character number, and cannot be a value or expression of other types.

The syntax for accessing array elements and reassigning values:

Array name [specify subscript value] = new value

Example:

Var arr = [1Jing 2 Jing 3 Jing 4 Jing 5]; / / declare an array console.log (arr); arr [0] = 0; / / modify the first element and reassign it to 0arr [2] = "A"; / / modify the third element and reassign it to 2console.log (arr)

Method 2: using splice () method

The splice () method of the Array array is also a very powerful method, which is used to delete, insert, and replace.

It should be noted that the splice () method modifies the original array directly.

We can modify the array elements by using the substitution function of this method.

Syntax:

Array.splice (starti,n, value 1, value 2)

What it really is is: delete n elements in the position of starti, and then insert a value of 1 and a value of 2 in this position to replace the value that was originally deleted.

Example

Var array = [1Jing 2Jing 3Jing 4Jing 5]; / / declare an array console.log (array); array.splice (2123456); console.log (array)

At this point, I believe you have a deeper understanding of "how ES6 modifies an element in the array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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