In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "Sample Analysis of Basic Operation of JavaScript Array". The content is simple and easy to understand. It is clearly organized. I hope it can help you solve your doubts. Let Xiaobian lead you to study and learn the "Sample Analysis of Basic Operation of JavaScript Array" article.
First, the initial knowledge array
Array Composition: An array consisting of one or more array elements separated by commas ",".
Array elements: Each array element consists of a "subscript" and a "value."
Subscript: Also known as index, expressed as a number, default from 0 in order to increase, used to identify elements.
Value: The content of an element, which can be any type of data, such as numeric, character, array, object, etc.
Arrays can also be divided into one-dimensional arrays, two-dimensional arrays, three-dimensional arrays and other multidimensional arrays according to dimensions.
One-dimensional array: refers to the array "value" is not an array type of data, as shown in the figure above.
Two-dimensional array: refers to the array element "value" is a one-dimensional array, as shown in the following figure.
Arrays can also be divided into one-dimensional arrays, two-dimensional arrays, three-dimensional arrays and other multidimensional arrays according to dimensions.
Multidimensional arrays: When an array's values are also an array, a multidimensional array can be formed. It is usually used to describe information.
For example: save a class of student information, each array element represents a student, and each student uses a one-dimensional array to represent their name, student number, age and other information, so that through a variable can have a regular save of all student information in a class, convenient development processing.
2. Create an array
How arrays are created in JavaScript:
How Array objects are instantiated.
Use "[]" directly.
Creating arrays using Array objects
Array objects are created by instantiating them, using the new keyword.
Create an array using "[]"
The direct method "[]" is used similarly to Array() objects, simply replacing new Array() with [].
When creating an array, the comma after the last element can be present or omitted.
The direct method "[]" differs from Array() objects in creating arrays in that the former can create arrays with empty storage locations, while the latter cannot.
The basic operations of an array get the length of the array
The Array object provides the length property to get the length of the array, and its value is the maximum subscript of the array element plus 1.
Array elements with no values in array arr2 occupy empty storage locations.
Therefore, the subscripts of the array are still incremented. So arr2 calls the length property and the final output is 6.
The length property of an array can be used not only to obtain the array length, but also to modify the array length.
When specifying the length of an array using the length attribute, there are three cases:
???? If the value of length is greater than the original number of elements in the array, empty storage locations are occupied by array elements with no values.
???? If the value of length is equal to the original number of elements in the array, the array length is unchanged.
???? If the value of length is less than the original number of elements in the array, the extra array elements will be discarded.
In addition, when creating an array using Array object methods, you can specify the length of the array.
note
Regardless of how the array length is specified in JavaScript, it does not affect the addition of elements to the array, and the value of the length attribute of the array will change accordingly.
Access array elements
Array element access method: "array name [subscript]".
Concept: Traversing an array is the operation of accessing all elements of an array in turn.
Using subscripts to traverse arrays can be used: for (learned).
Using subscripts to traverse an array can be done using the: for…in statement.
variable in for…in refers to an array subscript.
The object in for... in denotes the variable name of the array.
In addition, if an object is an object, for…in can also be used for object traversal.
note
In ES6, a new for…of syntax was added to make it easier to traverse arrays.
Variable value: Indicates the value of the array element corresponding to each iteration.
Variable arr: Indicates the array to be traversed.
Result: Output 1, 2, and 3 in the console.
Addition and modification of elements
Elements are added and modified in the following way: "array name [subscript]."
Tip: Same way you access elements in an array.
additive element
Add array elements: Array name [subscript] = value.
Allow subscripts not to be added consecutively in numerical order, and elements without specific values will exist in the form of empty storage locations.
The order in which elements are stored in an array depends on the subscript, not the order in which elements are added.
modify the element
Modifying an element is used the same way as adding an element, except that modifying an element reassigns an element that already has a value.
Deletion of elements
After creating an array, sometimes it is necessary to delete an element value in the array according to the actual situation.
For example, a multidimensional array that stores information about a class of students, if a student in this class transfers, then this student needs to be deleted from the array that stores information about students.
At this point, you can delete the value of the array element using the delete keyword.
The delete keyword can only delete the element value of the specified index in the array. After deletion, the element will still occupy an empty storage location.
destructuring assignment
In addition to the variable declaration and assignment methods studied earlier, ES6 also provides another way-deconstructing assignment. For example, if the elements in array [1, 2, 3] are assigned a, b, and c, the traditional approach is to declare and assign variables separately.
When the number of variables on the left is less than the number of elements on the right, the extra elements are ignored.
When the number of variables on the left is greater than the number of elements on the right, the extra variables are initialized to undefined.
The right-hand side of a deconstructed assignment can also be a variable.
Complete the exchange of values between two variables by deconstructing assignments.
The above is "JavaScript array basic operation of the sample analysis" all the content of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.