This example describes the use of array objects in JavaScript. Share to everyone for your reference, specific as follows:
Array arrays have many commonly used methods and properties, which are summarized as follows:
1. Length property, gets the number of elements in the array.
2. Concat () method, connecting two arrays. Connect the two arrays together. Examples are as follows:
var names= new Array (' Jack ', ' Tom ', ' Jim ');
var ages= new Array (12,32,44);
var ConcatArray;
Concatarray=names.concat (ages);
The ConcatArray here is a new array of name arrays and age arrays.
3. Slice () method to get some of the array elements in the array.
There are generally two parameters, the first one represents the starting position, and the second represents the end position (similar to substring). It is worth noting that the intercepted array element precedes the second parameter position. That is, if the second argument is 4, it means that before the fourth array element is intercepted.
4. Join () method to convert an array to a string. This method is a JavaScript method and is often used in jquery. Examples are as follows:
var myshopping=new Array ("Eggs", "apple", "milk");
var myshoppinglist = Myshopping.join ("<br>");
document.write (myShoppingList);
Here the myShoppingList is a string, the content is "Eggs<br>apple<br>milk";
5. The sort () method, in which the elements in an array are sorted, arranged in alphabetical order, from small to large.
6. Reverse () method, the elements in the array to flip, before and after the head off.
If you combine the sort () method with the reverse () method, you can achieve the effect of a reverse order.
That is, the first sort, and then flip, so that the effect of reverse order.
Here is a small synthesis example:
<script type= ' text/javascript ' >
var myshopping = new Array ("Eggs", "Milk", "potatoes", "Banana", "cereal");
var ord = parseint (Prompt ("Enter 1 for alphabetical order,and-1 to reverse order", 1));
Switch (ORD)
{case
1:
myshopping.sort ();
myshopping = Myshopping.join ("<br>");
document.write (myshopping);
break;
Case-1:
myshopping.sort ();
Myshopping.reverse ();
myshopping = Myshopping.join ("<br>");
document.write (myshopping);
break;
Default:
document.write ("That is not a valid input.");
break;
</script>
This small example is the use of sorting and flipping methods in array objects and a small example of join methods. If the input is 1, it is ordered and output, and if 1 is ordered in reverse order and output.
More readers interested in JavaScript-related content can view the site topics: "javascript array Operation tips Summary", "JavaScript traversal algorithm and Skills summary", "JavaScript Mathematical calculation Usage Summary", " JavaScript data structure and algorithm skills summary, "JavaScript switching effects and techniques summary", "JavaScript Search Algorithm Skills Summary", "JavaScript animation effects and techniques summary" and "JavaScript error and debugging skills Summary"
I hope this article will help you with JavaScript programming.