Array
I said earlier.,An array is also a type of object,remember that??What do you mean, arrays?,those basic characteristics of an array,how to store in memory,On the heap or on the stack?or the other.I forgot my own brain tonic..No, I'm not.,that programming language has arrays,Although the array is not very useful,and make sure that countless,But the data is the basis,It is believed that each person is a more advanced container that has been contacted after the array has been lifted.,What are the more useful containers??the kind of container that corresponds to that different data is more appropriate.?own Brain Repair.
Today I'm talking about arrays in JS (array) .
See example : Create an array
<script>
var array=new array ();
Array[0]= "Syx";
Array[1]= "Nan";
array[2]=22;
for (i =0;i<array.length;++i)
{
document.write (array[i]+ "<br/>")
}
</script>
To iterate through an array using for/in :
for (x in array)
{
document.write (array[x]+ "<br/>");
}
Merging two arrays : using method concat
<script>
var arr1=new Array (3);
var arr2=new Array (3);
Arr1[0]= "Syx";
Arr1[1]= "Nan";
arr1[2]=22;
Arr2[0]= "ZSF";
Arr2[1]= "NV";
arr2[2]=22;
document.write (Arr1.concat (ARR2)); What is //document ? this is equivalent to an output statement , Understand ?
</script>
To compose a string with elements of an array : using the join () method
<script>
var arr = new Array (3);
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
document.write (Arr.join ());// use commas as separators by default
document.write ("<br/>");
document.write (Arr.join (".")); / The delimiter is specified here , using the dot number (.)
</script>
Sort an array using sort :
<script type= "Text/javascript" >
This is to sort the array of numbers using the sort method
function Sortnumber (A, B)
{
return a-B;
}
var arr=new Array (6);
Arr[0] = "10"
ARR[1] = "5"
ARR[2] = "40"
ARR[3] = "25"
Arr[4] = "1000"
ARR[5] = "1"
document.write (arr + "<br/>");// This is a result of no sorting
document.write (Arr.sort (Sortnumber));// This is the result of sorting
document.write ("<br/>");
document.write ("<br/>");
document.write ("<br/>");
to sort a string array using the sort method
var arr = new Array (6)
Arr[0] = "George"
ARR[1] = "John"
ARR[2] = "Thomas"
ARR[3] = "James"
ARR[4] = "Adrew"
ARR[5] = "Martin"
document.write (arr + "<br/>")
document.write (Arr.sort ())
I don't know if you noticed. The default ordering of strings is sorted by the number of dictionaries
</script>
These are just a few examples of common methods . Here's a more detailed look. Array properties and methods of arrays
Array Object
The Array object is used to store multiple values in a single variable .
Create The syntax for the array object , and if you notice how I created the array earlier , You are a conscientious person .
In fact , There are three ways to create an array :
1.new Array ();
2.new Array (size);
3.new Array (element0,,element1,... elementn);
Parameters
Parameter size is the number of specified array elements Returns an array of ,length field will be set to size
The parameter element1...elementn is a list of parameters . When you use these parameters to call the constructor Array () when , The elements of the newly created array are initialized to these values . of his length The field will also be set to the number of parameters .
return value
Returns the newly created and initialized array .
If no arguments are used when calling the constructor Array () , then the returned array is empty and the length field is 0
When the call constructor value is passed to a numeric parameter , The constructor returns An array with the number of undefined elements . .
When an array () is called by another parameter , The constructor initializes the array with the value specified by the parameter .
When a constructor is called as a function, and the new operator is not used , his behavior is to use the new The operator invokes it exactly as it behaves .
Array Object Properties
Property name |
Describe |
Constructor |
Returns a reference to the array function that created this object |
Length |
Sets or returns a serious array in an array |
Prototype |
gives you the ability to add properties and methods to objects |
Array Object Method
Method name |
Describe |
Concat () |
Concatenate two or more arrays and return the result |
Join () |
Puts all elements of an array into a string , and the elements are split by the specified delimiter |
Pop () |
Delete and return the last element of the array |
Push () |
Adds one or more elements to the end of the array and returns the new length |
Reverse () |
Reverses the order of elements in an array |
Shift () |
Delete and return the first element of the array |
Slice () |
Returns the selected element from an existing array |
Sort () |
Sorting elements of an array |
Splice () |
Delete the element and add a new element to the array |
Tosource () |
Returns the source code of the object |
ToString () |
Converts an array to a string and returns the result |
toLocaleString () |
Converts an array to a local array and returns the result |
Unshift () |
Adds one or more elements to the beginning of the array and returns the length |
ValueOf () |
Returns the original value of an array object |
Defining arrays
An array object is used to store a series of values in a separate variable name .
We use the new keyword to create an array object . Case :
var array =new Array ()
There are two ways to assign values to an array ( You can add as many values as you want to define as many variables as you need ).
The first type :
var array=new arrau ()
Array[0]=1;
array[1]=2;
array[2]=3;
You can also use an integer argument to control the capacity of an array :
var array=new array (3)
Array[0]=1;
array[1]=2;
array[2]=3;
The second type :
var mycars=new Array ("Saab", "Volvo", "BMW")
Note : If you need to specify numeric or logical values within an array , then the variable type should be a numeric or Boolean variable , not a character variable .
accessing arrays
By specifying the array name and the index number , You can access a particular element .
Case :
document.write (Array[0]);
Output :Saab
To modify the values in an array
If you modify a value in an existing array , simply add a new value if you want to specify the subscript :
array[0]= "hahaha";
Test Code :
document.write (Array[0]);
Output is : Hahaha
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
JS Learning nineth Day----array