We should be familiar with the concept of arrays. As early as in our school's mathematics, we have explained in detail the definition and usage of arrays, the definition and use of arrays are slightly different in programming languages. In addition, there are no attributes and methods of arrays in mathematics. In fact, when we explain the PHP language, we have already involved the concepts and usage of arrays in programming languages. Our tutorial today is only to deepen this concept, list some common attributes and methods of arrays. In the previous chapter, we mainly discussed date objects in detail. If you haven't come or read the previous article, so here we have a shortcut channel "Detailed description of date objects in JavaScript".
Let's take a look at the standard statements defining Arrays:
- // Define the standard statement of the array
- VaR array name = new array ()
After defining an array, how do we assign values to the array? That is to say, how do we add elements to the newly created array?
In fact, there are three ways to create an array. The first and second methods are similar. The only difference is that the number of elements is added to the array parameters. See the following code:
- // When creating an array, we can not add the number of elements to the array parameter.
- // The first sentence of the following code can also be written:
- // Var carsname = new array ()
- // Zihan interactive visual original tutorial. reposted the original article. Thank you for your cooperation.
- VaR carsname = new array (4)
- Carsname [0] = "BMW"
- Carsname [1] = "Mercedes-Benz"
- Carsname [2] = "Land Rover"
- Carsname [3] = "Hummer"
In addition to the above two methods, we can also directly write the elements of the array as parameters. The Code is as follows:
- VaR carsname = new array ("BMW", "Mercedes-Benz", "Land Rover", "Hummer ")
After creating an array, we may need to access its array elements. below is its standard access statement:
- // Access the first element of the array
- Document. Write (carsname [0])
In addition to accessing array elements, sometimes we also need to re-assign values or modify the elements of the array. Please refer to the following code:
- // Based on the above Code
- // The original first element is BMW.
- // After the value is assigned again
- // The value of the first element of the array is Bentley.
- Mycars [0] = "Bentley ";
After learning about the basic usage of arrays, let's take a look at several methods of arrays. For details, see the following:
1. Use the Concat () method to merge two Arrays:
- <HTML>
- <Body>
- // Use the Concat () method to merge two arrays.
- <SCRIPT type = "text/JavaScript">
- // The first array contains three elements.
- VaR myav = new array (3)
- Arr [0] = "cangjingkong"
- Arr [1] = "Ma shengxi"
- Arr [2] = "podoo closed clothing"
- // The second array also contains three elements.
- VaR myav2 = new array (3)
- Arr2 [0] = "hongyin Ying"
- Arr2 [1] = "grapefruit under wood"
- Arr2 [2] = "jizemingbu"
- // Merge the two Arrays
- Document. Write (myav. Concat (myav2 ))
- // The final output result should be:
- // Cangjingkong, Ma shengxi, Bo duoye Jieyi, hongyin Ying, Mu xianyu, Ji zemingbu
- </SCRIPT>
- </Body>
- </Html>
2. Use the join () method to form a string of all elements in the array. See the following sample code:
- <HTML>
- <Body>
- // Combine all elements in the array into a string
- <SCRIPT type = "text/JavaScript">
- // Create an array containing three elements
- VaR mystory = new array (3 );
- Mystory [0] = "Sunshine"
- Mystory [1] = "moonlight"
- Mystory [2] = "Guang guang"
- // Concatenate array elements into strings
- // The output result is: sunlight, moonlight, and light.
- Document. Write (mystory. Join ());
- Document. Write ("<br/> ");
- // Change the delimiter between strings
- // The output result is sunlight, moonlight, and light.
- Document. Write (mystory. Join ("."));
- </SCRIPT>
- </Body>
- </Html>
3. use the sort () method to sort the elements in the array. This sort will be explained in two forms, one is to sort the letters, the other is to sort numbers. First, let's first look at how to sort the letters (sort by the first letter. When the first character is the same, from the second place), such as the following code:
- <HTML>
- <Body>
- // Sorts array elements alphabetically.
- <SCRIPT type = "text/JavaScript">
- // Create an array with six elements
- VaR demo = new array (6)
- Demo [0] = "cat"
- Demo [1] = "car"
- Demo [2] = "more"
- Demo [3] = "TOP"
- Demo [4] = "first"
- Demo [5] = "God"
- Document. Write (Demo + "<br/> ")
- // If sort () is not set, the default values are sorted alphabetically.
- // When the first letter is the same, the comparison is still extended from the second character
- Document. Write (Demo. Sort ())
- // Because the result is: car, Cat, first, God, more, top
- </SCRIPT>
- </Body>
- </Html>
Next let's take a look at the sorting of numbers, so we have to add parameters to the sort () method. Please refer to the following example:
- <HTML>
- <Body>
- <SCRIPT type = "text/JavaScript">
- Function demonumber (x, y)
- {
- Return x-y
- }
- VaR arrdemo = new array (6)
- Arrdemo [0] = "300"
- Arrdemo [1] = "60"
- Arrdemo [2] = "75"
- Arrdemo [3] = "80"
- Arrdemo [4] = "980"
- Arrdemo [5] = "66"
- Document. Write (arrdemo + "<br/> ")
- // Note that its parameters are the functions created above
- // Result: 300,980
- Document. Write (arrdemo. Sort (demonumber ))
- </SCRIPT>
- </Body>
- </Html>
4. ...... The in statement determines whether an element is in an array. See the following example:
- <HTML>
- <Body>
- // Array traversal usage
- <SCRIPT type = "text/JavaScript">
- VaR I
- VaR carsdemo = new array ()
- Carsdemo [0] = "Ford"
- Carsdemo [1] = "Chevrolet"
- Carsdemo [2] = "Peugeot"
- For (I in carsdemo)
- {
- Document. Write (carsdemo [I] + "<br/> ")
- }
- </SCRIPT>
- </Body>
- </Html>
<HTML>
<Body>
<SCRIPT type = "text/JavaScript">
Function demonumber (x, y)
{
Return x-y
}
VaR arr = new array (6)
Arr [0] = 3
Arr [1] = 9
Arr [2] = 5
Arr [3] = 12
Arr [4] = 6
Arr [5] = 1
Arr [5] = 2
Document. Write (ARR + "<br/> ")
Document. Write (ARR. Sort (demonumber ))
</SCRIPT>
</Body>
</Html>
From http://www.xmlas.com/javascript-array-object.html