Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
On the literature to JavaScript array object, this time to learn the array object common method, this knowledge point more, more complex, but also more important!
Array Array object (1)
1. Add array elements:
The length of an array can be changed at will, and there are two ways to add an array element. In JavaScript, array element sequence number I starts at 0, that is, the subscript I of the first element is 0, followed by 1
1.1 You can value or assign an array element in this way. The syntax is as follows:
Array variable name = value variable name = array variable name [I]Copy the code
1.2 There are two ways to add an array element:
- Modify the length property of the array
An array of length 3 can add three elements from the end of the array by setting length = 6. Each new element is undefined
- Assign values directly to the elements of an array, for example
const arr = []
arr[0] = 6
arr[1] = 2
arr[2] = 1
arr[3] = 621
Copy the code
2. Drop array elements:
2.1 throughlength
Removing elements from the end is like adding
You can remove an array element from the end of an array by modifying its length attribute.
An array of length 6 can remove the last three elements from the end by setting length = 3
2.2 Using the DELETE operator
JavaScript provides the delete operator to delete an array element anywhere in the array, but changing the element to undefined is not a true delete, so the array length does not change.
To actually delete an element, you can use more array methods, such as the.splice() method. Behind the study