javascript operation array details

  • 2020-05-07 19:12:37
  • OfStack

1. Creation of array


var arrayObj = new Array(); // create 1 An array
var arrayObj = new Array([size]); // create 1 And specify the length, not the upper bound, but the length
var arrayObj = new Array([element0[, element1[, ...[, elementN]]]]); // create 1 Array and assign values

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 ]]]]);// will 1 Two or more new elements are added to the end of the array and the new length of the array is returned
arrayObj.unshift([item1 [item2 [. . . [itemN ]]]]);// will 1 Two or more new elements are added to the beginning of the array. The elements in the array are automatically moved back to return the new length of the array
arrayObj.splice(insertPos,0,[item1[, item2[, . . . [,itemN]]]]);// will 1 Four or more new elements are inserted into the specified position of the array "" .

4. Delete array elements


arrayObj.pop(); // Remove the last 1 And returns the value of that element
arrayObj.shift(); // Remove the former 1 Element and returns the value of that element. The elements in the array move forward automatically
arrayObj.splice(deletePos,deleteCount); // Deletes from the specified location deletePos The specified number to begin with deleteCount Returns the removed element as an array

5. Intercept and merge arrays


arrayObj.slice(start, [end]); // Returns an array as an array 1 Partial, not included end The corresponding element, if omitted end Will be copied start Everything after that
arrayObj.concat([item1[, item2[, . . . [,itemN]]]]); // Concatenate multiple arrays (or strings, or a mix of arrays and strings) to 1 Array, return the concatenated new array

6. Copy of the array


arrayObj.slice(0); // Returns a copy of the array array, note yes 1 A new array, not a point
arrayObj.concat(); // Returns a copy of the array array, note yes 1 A new array, not a point

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 will hold each of the arrays 1 The element values are concatenated in 1 Start and use in the middle separator Separated.
toLocaleString , toString , valueOf : it can be regarded as join Not commonly used

2. Three properties of the array object

1. length properties

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 upper and lower limits of an array are: 0 and length-1, respectively. Unlike most other languages, the length attribute of the JavaScript array is mutable, which is a point to note. When the length attribute is set larger, the state of the entire array does not actually change, only the length attribute increases. When the length attribute is set smaller than the original value, the value of all elements in the original array whose index is greater than or equal to length is lost. Here is an example of changing the length attribute:


var arr=[12,23,5,3,25,98,76,54,56,76];
// Defines the 1 containing 10 An array of Numbers
alert(arr.length); // Displays the length of the array 10
arr.length=12; // Increases the length of the array
alert(arr.length); // The length of the display array has been changed to 12
alert(arr[8]); // According to the first 9 Is the value of 56
arr.length=5; // Reduce the length of the array to 5 , index equals or exceeds 5 Is discarded
alert(arr[8]); // According to the first 9 The elements have been changed into "undefined"
arr.length=10; // Restores the array length to 10
alert(arr[8]); // Although the length is restored to zero 10 But the first 9 Element but cannot be retrieved, display "undefined"

From the above code we can clearly see the nature of length property. But not only can the length object be set explicitly, it can also be modified implicitly. You can use 1 undeclared variable in JavaScript, and you can also use 1 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 quoted by the element 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);

In the code, we also defined an array containing 10 Numbers, whose length is 10 according to alert statement. We then used an element with an index of 15 and assigned it to 15, i.e., arr[15]=34. Then we printed the length of the array with the alert statement and got 16. In any case, this is a surprising feature for developers used to strongly typed programming. In fact, an array created in the form new Array() has an initial length of 0, and it is the operation of an undefined element in it 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, an in-depth understanding of length properties can be used flexibly in the development process.

2. prototype attribute

Returns a reference to an object type stereotype. The prototype attribute is common to object.

objectName.prototype

The objectName parameter is the name of the object object.

Note: the prototype attribute provides 1 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 following example illustrates the use of the prototype attribute.

Adds a method to an array object that returns the value of the largest element in the array. To complete this point, 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.

Note: the constructor attribute is a member of all objects with prototype. They include all JScript native objects except Global and Math objects. The constructor property holds a reference to a function that constructs a particular 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).

For arrays:


y = new Array();

The above is my understanding of the array operation method of javascript. If you find any errors, please point them out.


Related articles: