Use of JS array arrays

Source: Internet
Author: User

use of JS array arrays
  1. An array is a pre-defined object (also known as a Class) in JavaScript that you can use directly
  2. Create an Array object
  3. var array=new Array ();
  4. Creates an array object of the specified number of elements
  5. var array=new Array (23°c);
  6. Creates an array object with the specified element:
  7. var array=new Array (' Zhang Sa ',' John Doe ',' logistics ',' Zhang Yi ');
  8. Outputs the values of all elements in an array object
  9. For ... inch
  10. var array=new Array (' Zhang Sa ',' John Doe ',' logistics ',' Zhang Yi ');
  11. For (var index in Array)
  12. {
  13. Alert (Array[index])
  14. }
  15. Add an element to an existing array object
  16. array[5]=' Tianqi ';
  17. Regardless of whether the array object has a size defined beforehand, you can add elements to it, if the element is not added in indexed order, the middle
  18. Element will be filled with null
  19. Replace original element
  20. array[2]="123";
  21. directly specifies the index of the array element to be replaced, and the direct assignment can be substituted
  22. Another way to declare an array object,
  23. Do not apply the new keyword, but instead use [] to include the value of the element directly .
  24. This is the same effect as using the New keyword
  25. var array =[' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu '];
  26. Make all the elements in the array a string toString () method:
  27. Concatenate all elements with a "," number to form a string.
  28. var array = [' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu '];
  29. Alert (array.tostring ());
  30. If you want to use a custom symbol as the delimiter for an element in an array object
  31. You can use join
  32. split with "-"
  33. var array = [' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu '];
  34. Alert (Array.join ('-'));
  35. split with "/"
  36. var array = [' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu '];
  37. Alert (Array.join ('/'));
  38. Convert string to array object (array)
  39. A very familiar approach.
  40. var str = ' Zhang San, John Doe, Harry, Zhao Liu ';
  41. var array=str.split (', ');
  42. Alert (array.tostring ());
  43. What if the above example is split with a space?
  44. The following statement uses a space to split it? The use of God horse?
  45. var str = ' dick and Harry Harry Zhao Liu ';
  46. var array=str.split (');
  47. Alert (array.tostring ());
  48. 2 Useful methods
  49. Concat (): Adds an element at the end of the array and returns a new array, unchanged from the original array.
  50. var array = new Array (' Zhang San, John Doe, Harry, Zhao Liu ');
  51. var array1= array.concat (' Tianqi ', ' Wang Ba ');
  52. Alert (array.tostring ());
  53. Alert (array1.tostring ());
  54. Note: The difference between adding elements directly to the array and the front:
  55. The former operates on the array itself, while the latter is the return of a new array, the meta-array unchanged
  56. Slice ():
  57. Intercepts the specified element from the array and returns the new array
  58. The original array does not change
  59. Intercept starts from the 2nd element to the end of the last element
  60. var array = new Array (' Zhang San ',' John Doe ',' Harry ',' Zhao Liu ');
  61. var array1 = Array.slice (1);
  62. Alert (array1.tostring ());
  63. Push (): Adds one or more items at the end of the array
  64. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  65. Array.push (' Tianqi ');
  66. Array.push (' Wang Ba ',' old nine ',' old 10 ');
  67. Alert (array.tostring ());
  68. The previous method of adding an element has the same effect.
  69. Pop
  70. Deletes the last item of the array and returns it as a return value
  71. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  72. var lastvalue = Array.pop ();
  73. alert (Lastvalue);
  74. Alert (array.tostring ());
  75. Note: The last item is deleted
  76. Shift
  77. Deletes the first item of the array and returns it as a return value
  78. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  79. var firstvalue = Array.shift ();
  80. alert (firstvalue);
  81. Alert (array.tostring ());
  82. Note: The first item is deleted
  83. Unshift
  84. Put an item in the first position of the array,
  85. Then push the rest of the items back one position.
  86. See below for code.
  87. The parameters of the Unshift function can also be multiple,
  88. This will put the first parameter in the first position,
  89. The second argument is placed in the position of the second item, followed by a shift, and the code below
  90. Reverse
  91. Reverse the order of the array
  92. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  93. Array.reverse ();
  94. document.write (Array.tostring ());
  95. Sort: Sorting
  96. var array = new Array ( ' John Doe ',' Zhang San ', ' Zhao Liu ',' Harry ');
  97. Array.Sort ();
  98. document.write (Array.tostring ());
  99. Splice () Method:
  100. Simple and easy to use
  101. The second argument is 0 o'clock, and no items are deleted
  102. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  103. Array.splice (1, 0);
  104. document.write (Array.tostring ());
  105. The second argument is 1 o'clock, remove the element that specifies subscript for the first parameter
  106. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  107. Array.splice (1, 1);
  108. document.write (Array.tostring ());
  109. The second argument is 2 o'clock,
  110. Delete the subscript that specifies the subscript for the first parameter and the subscript for the first parameter, +1,
  111. The following
  112. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  113. (Array.splice);
  114. document.write (Array.tostring ());
  115. The second parameter is 0 o'clock, and a new one is added to the position subscript 1.
  116. The element that originally subscript 1 is moved back
  117. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  118. Array.splice (1,0,' new additions ');
  119. document.write (Array.tostring ());
  120. The second parameter is 1 o'clock, and a new one is added at the subscript 1, and the element with the original subscript 1 is deleted.
  121. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  122. Array.splice (1, 1, ' new additions ');
  123. document.write (Array.tostring ());
  124. The second parameter is 2 o'clock, the position of the subscript is 1 new, the original subscript is 1 and subscript for the element of the delete,
  125. The following
  126. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  127. Array.splice (1, 2, ' new additions ');
  128. document.write (Array.tostring ());
  129. Deleted 2 items, added 2 items
  130. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  131. Array.splice (,' first ',' second ');
  132. document.write (Array.tostring ());


Use of JS array arrays

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.