Javascript array operation of of creation element deletion copy of the array
- 2020-03-30 02:33:44
- OfStack
1. Creation of array
var arrayObj = new Array(); //Create an array
var arrayObj = new Array([size]); //Create an array And specify the length, not the upper bound, the length
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); Create an array And the assignment
Note that while the second method of creating an array specifies a length, in all cases the array actually gets longer, which means that even if you specify a length of 5, you can still store the element outside the specified length. Note that the length changes.
2. Access to the elements of the array
var testGetArrValue=arrayObj[1]; //Gets the element value of the array
arrayObj[1]= " This is the new value "; //Assign a new value to an array element
3. Adding array elements
arrayObj. push([item1 [item2 [. . . [itemN ]]]]);//Adds one or more new elements to the end of the array and returns the new length of the array
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);//When one or more new elements are added to the beginning of the array, the elements in the array automatically move back, returning the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);//Inserts one or more new elements into the specified position in the array. The inserted element automatically moves back, returning "".
4. Delete array elements
arrayObj.pop(); //Removes the last element and returns the value of that element
arrayObj.shift(); //Removes the previous element and returns the value of that element. Elements in the array move forward automatically
arrayObj.splice(deletePos,deleteCount); //Deletes the specified number of deleteCount elements starting from the specified location deletePos, and returns the removed elements as an array
5. Intercept and merge arrays
arrayObj.slice(start, [end]); //Returns a portion of the array in the form of an array. Note that the element corresponding to the end is not included
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); //Concatenate multiple arrays (which can be strings, or a mix of arrays and strings) into one array, returning a new concatenated array
6. Copy of the array
arrayObj.slice(0); //Returns a copy of the array. Note that it is a new array, not a pointer
arrayObj.concat(); //Returns a copy of the array. Note that it is a new array, not a pointer
7. Sorting array elements
arrayObj.reverse(); //Invert the element (first to last, last to first) to return the array address
arrayObj.sort(); //Sort the array elements and return the array address
8. Stringing of array elements
arrayObj.join(separator); //Returns a string that concatenates the values of each element of the array, separated by separator.
ToLocaleString, toString, valueOf: can be thought of as a special use of join, not commonly used
Two, the array object of the three properties
1. Length attribute
The Length attribute represents the Length of the array, which is the number of elements in it. Since the index of an array always starts at 0, the bounds of an array are 0 and length-1, respectively. Unlike most other languages, JavaScript arrays have a variable length property, which is something to be careful about. When the length property is set larger, the state of the entire array does not actually change, just the length property. When the length attribute is set smaller than the original value, the values of elements in the original array whose index is greater than or equal to the length are all lost. Here is an example of changing the length property:
var arr=[12,23,5,3,25,98,76,54,56,76];
//Defines an array of 10 Numbers
alert(arr.length); //Displays the length of the array 10
arr.length=12; //Increases the length of the array
alert(arr.length); //The display array length has changed to 12
alert(arr[8]); //Displays the value of the ninth element, which is 56
arr.length=5; //Reduce the length of the array to 5, and elements with an index equal to or greater than 5 are discarded
alert(arr[8]); //It says that the ninth element has been changed to "undefined"
arr.length=10; //Restore the array length to 10
alert(arr[8]); //The length is restored to 10, but the ninth element cannot be retrieved, so it says "undefined"
From the above code we can clearly see the nature of the length property. But the length object can not only be set explicitly, it can also be modified implicitly. JavaScript can use an undeclared variable, as well as an undefined array element (an element whose index is greater than or equal to length), where the value of the length attribute is set to the value of the index of the element being used plus 1. For example, the following code:
var arr=[12,23,5,3,25,98,76,54,56,76];
alert(arr.length);
arr[15]=34;
alert(arr.length);
The code also defines an array of 10 Numbers, whose length is 10 through the alert statement. We then used an element with an index of 15, assigned it to 15, that is, arr[15]=34, and then printed the length of the array with an alert statement, which gives us 16. In any case, this is a surprising feature for developers used to strongly typed programming. In fact, the initial length of an Array created in the form of new Array() is 0, and it is the operation on the undefined elements that changes the length of the Array.
As you can see from the above, the length attribute is so magical that it can be easily used to increase or decrease the size of an array. Therefore, a deep understanding of the length attribute can be used flexibly in the development process.
2. Prototype property
Returns a reference to an object type stereotype. The prototype property is Shared with object.
The objectName. Prototype
The objectName parameter is the name of the object object.
Note: the prototype property provides a set of basic functionality of the object's class. A new instance of an object "inherits" the operations assigned to the object stereotype.
For array objects, the prototype property is used as an example.
Adds a method to an array object that returns the value of the largest element in the array. To accomplish this, declare a function, add it to array.prototype, and use it.
function array_max( )
{
var i, max = this[0];
for (i = 1; i < this.length; i++)
{
if (max < this[i])
max = this[i];
}
return max;
}
Array.prototype.max = array_max;
var x = new Array(1, 2, 3, 4, 5, 6);
var y = x.max( );
After this code is executed, y holds the maximum value in the array x, or 6.
3. Constructor attribute
Represents a function that creates an object.
object.constructor //Object is the name of an object or function.
The constructor property is a member of any object with a prototype. They include all JScript native objects except Global and Math objects. The constructor attribute holds a reference to a function that constructs a specific object instance.
Such as:
x = new String("Hi");
if (x.constructor == String) //Process (condition true).
or
function MyFunc {
//The body of the function.
}
y = new MyFunc;
if (y.constructor == MyFunc) //Process (condition true).