Array of JavaScript objects

Source: Internet
Author: User

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:

  1. // Define the standard statement of the array
  2. 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:

  1. // When creating an array, we can not add the number of elements to the array parameter.
  2. // The first sentence of the following code can also be written:
  3. // Var carsname = new array ()
  4. // Zihan interactive visual original tutorial. reposted the original article. Thank you for your cooperation.
  5. VaR carsname = new array (4)
  6. Carsname [0] = "BMW"
  7. Carsname [1] = "Mercedes-Benz"
  8. Carsname [2] = "Land Rover"
  9. 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:

  1. 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:

  1. // Access the first element of the array
  2. 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:

  1. // Based on the above Code
  2. // The original first element is BMW.
  3. // After the value is assigned again
  4. // The value of the first element of the array is Bentley.
  5. 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:

  1. <HTML>
  2. <Body>
  3. // Use the Concat () method to merge two arrays.
  4. <SCRIPT type = "text/JavaScript">
  5. // The first array contains three elements.
  6. VaR myav = new array (3)
  7. Arr [0] = "cangjingkong"
  8. Arr [1] = "Ma shengxi"
  9. Arr [2] = "podoo closed clothing"
  10. // The second array also contains three elements.
  11. VaR myav2 = new array (3)
  12. Arr2 [0] = "hongyin Ying"
  13. Arr2 [1] = "grapefruit under wood"
  14. Arr2 [2] = "jizemingbu"
  15. // Merge the two Arrays
  16. Document. Write (myav. Concat (myav2 ))
  17. // The final output result should be:
  18. // Cangjingkong, Ma shengxi, Bo duoye Jieyi, hongyin Ying, Mu xianyu, Ji zemingbu
  19. </SCRIPT>
  20. </Body>
  21. </Html>

2. Use the join () method to form a string of all elements in the array. See the following sample code:

  1. <HTML>
  2. <Body>
  3. // Combine all elements in the array into a string
  4. <SCRIPT type = "text/JavaScript">
  5. // Create an array containing three elements
  6. VaR mystory = new array (3 );
  7. Mystory [0] = "Sunshine"
  8. Mystory [1] = "moonlight"
  9. Mystory [2] = "Guang guang"
  10. // Concatenate array elements into strings
  11. // The output result is: sunlight, moonlight, and light.
  12. Document. Write (mystory. Join ());
  13. Document. Write ("<br/> ");
  14. // Change the delimiter between strings
  15. // The output result is sunlight, moonlight, and light.
  16. Document. Write (mystory. Join ("."));
  17. </SCRIPT>
  18. </Body>
  19. </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:

  1. <HTML>
  2. <Body>
  3. // Sorts array elements alphabetically.
  4. <SCRIPT type = "text/JavaScript">
  5. // Create an array with six elements
  6. VaR demo = new array (6)
  7. Demo [0] = "cat"
  8. Demo [1] = "car"
  9. Demo [2] = "more"
  10. Demo [3] = "TOP"
  11. Demo [4] = "first"
  12. Demo [5] = "God"
  13. Document. Write (Demo + "<br/> ")
  14. // If sort () is not set, the default values are sorted alphabetically.
  15. // When the first letter is the same, the comparison is still extended from the second character
  16. Document. Write (Demo. Sort ())
  17. // Because the result is: car, Cat, first, God, more, top
  18. </SCRIPT>
  19. </Body>
  20. </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:

  1. <HTML>
  2. <Body>
  3. <SCRIPT type = "text/JavaScript">
  4. Function demonumber (x, y)
  5. {
  6. Return x-y
  7. }
  8. VaR arrdemo = new array (6)
  9. Arrdemo [0] = "300"
  10. Arrdemo [1] = "60"
  11. Arrdemo [2] = "75"
  12. Arrdemo [3] = "80"
  13. Arrdemo [4] = "980"
  14. Arrdemo [5] = "66"
  15. Document. Write (arrdemo + "<br/> ")
  16. // Note that its parameters are the functions created above
  17. // Result: 300,980
  18. Document. Write (arrdemo. Sort (demonumber ))
  19. </SCRIPT>
  20. </Body>
  21. </Html>

4. ...... The in statement determines whether an element is in an array. See the following example:

  1. <HTML>
  2. <Body>
  3. // Array traversal usage
  4. <SCRIPT type = "text/JavaScript">
  5. VaR I
  6. VaR carsdemo = new array ()
  7. Carsdemo [0] = "Ford"
  8. Carsdemo [1] = "Chevrolet"
  9. Carsdemo [2] = "Peugeot"
  10. For (I in carsdemo)
  11. {
  12. Document. Write (carsdemo [I] + "<br/> ")
  13. }
  14. </SCRIPT>
  15. </Body>
  16. </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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.