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. ,
  59. The original array does not change
  60. Intercept starts from the 2nd element to the end of the last element
  61. var array = new Array (' Zhang San ',' John Doe ',' Harry ',' Zhao Liu ');
  62. var array1 = Array.slice (1);
  63. Alert (array1.tostring ());
  64. Push (): Adds one or more items at the end of the array
  65. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  66. Array.push (' Tianqi ');
  67. Array.push (' Wang Ba ',' old nine ',' old 10 ');
  68. Alert (array.tostring ());
  69. The previous method of adding an element has the same effect.
  70. Pop
  71. Deletes the last item of the array and returns it as a return value
  72. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  73. var lastvalue = Array.pop ();
  74. alert (Lastvalue);
  75. Alert (array.tostring ());
  76. Note: The last item is deleted
  77. Shift
  78. Deletes the first item of the array and returns it as a return value
  79. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  80. var firstvalue = Array.shift ();
  81. alert (firstvalue);
  82. Alert (array.tostring ());
  83. Note: The first item is deleted
  84. Unshift
  85. Put an item in the first position of the array,
  86. Then push the rest of the items back one position.
  87. See below for code.
  88. The parameters of the Unshift function can also be multiple,
  89. This will put the first parameter in the first position,
  90. The second argument is placed in the position of the second item, followed by a shift, and the code below
  91. Reverse
  92. Reverse the order of the array
  93. var array = new Array (' Zhang San ', ' John Doe ', ' Harry ', ' Zhao Liu ');
  94. Array.reverse ();
  95. document.write (Array.tostring ());
  96. Sort: Sorting
  97. var array = new Array ( ' John Doe ',' Zhang San ', ' Zhao Liu ',' Harry ');
  98. Array.Sort ();
  99. document.write (Array.tostring ());
  100. Splice () Method:
  101. Simple and easy to use
  102. The second argument is 0 o'clock, and no items are deleted
  103. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  104. Array.splice (1, 0);
  105. document.write (Array.tostring ());
  106. The second argument is 1 o'clock, remove the element that specifies subscript for the first parameter
  107. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  108. Array.splice (1, 1);
  109. document.write (Array.tostring ());
  110. The second argument is 2 o'clock,
  111. Delete the subscript that specifies the subscript for the first parameter and the subscript for the first parameter, +1,
  112. The following
  113. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  114. (Array.splice);
  115. document.write (Array.tostring ());
  116. The second parameter is 0 o'clock, and a new one is added to the position subscript 1.
  117. The element that originally subscript 1 is moved back
  118. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  119. Array.splice (1,0,' new additions ');
  120. document.write (Array.tostring ());
  121. 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.
  122. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  123. Array.splice (1, 1, ' new additions ');
  124. document.write (Array.tostring ());
  125. 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,
  126. The following
  127. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  128. Array.splice (1, 2, ' new additions ');
  129. document.write (Array.tostring ());
  130. Deleted 2 items, added 2 items
  131. var array = new Array (' John Doe ', ' Zhang San ', ' Zhao Liu ', ' Harry ');
  132. Array.splice (,' first ',' second ');
  133. 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.