JavaScript Learning (a) array manipulation

Source: Internet
Author: User
Tags javascript array

, creation of arrays

[JavaScript]View Plaincopy
    1. var arrayobj = new Array (); //Create an array
    2. var arrayobj = New Array ([size]); //Create an array and specify the length, note not the upper limit, is the length
    3. var arrayobj = new Array ([element0[, element1[, ... [, ELEMENTN]]]); Create an array and assign a value





To illustrate, although the second method creates an array that specifies the length, the array is actually variable in all cases, meaning that even if the length is 5, the element can still be stored outside the specified length, note that the length changes accordingly.



2. Access to elements of an array


[JavaScript]View Plaincopy
    1. var testgetarrvalue=arrayobj[1]; //Gets the element value of the array
    2. arrayobj[1]= "This is the new value"; //Assign a new value to the array element





3. Adding array elements

[JavaScript]View Plaincopy
    1. 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
    2. Arrayobj.unshift ([Item1 [item2 [...] [Itemn]]); //Add one or more new elements to the beginning of the array, the elements in the array automatically move back, returning the new length of the array
    3. Arrayobj.splice (insertpos,0,[item1[, item2[, ... [, Itemn]]]);  //Inserts one or more new elements into the specified position of the array, the elements of the insertion position are automatically moved back, and the "" is returned.





4. Deletion of array elements

[JavaScript]View Plaincopy
    1. Arrayobj.pop (); //Remove the last element and return the element value
    2. Arrayobj.shift (); //Remove the first element and return the element value, the elements in the array are automatically moved forward
    3. Arrayobj.splice (Deletepos,deletecount); //Deletes the specified number of DeleteCount elements from the specified position deletepos, returning the removed element as an array





5. Interception and merging of arrays

[JavaScript]View Plaincopy
    1. Arrayobj.slice (start, [end]); //Returns the part of the array as an array, noting that the element of end is not included, and that if you omit end, all elements after start are copied
    2. Arrayobj.concat ([item1[, item2[, ... [, Itemn]]]); //To concatenate multiple arrays (which can also be strings, or a mixture of arrays and strings) into an array, returning a new concatenated array





6, copy of the array

[JavaScript]View Plaincopy
    1. Arrayobj.slice (0); //Returns a copy array of the array, note that it is a new array, not a pointer to the
    2. Arrayobj.concat (); //Returns a copy array of the array, note that it is a new array, not a pointer to the





7. Sorting of array elements

[JavaScript]View Plaincopy
    1. Arrayobj.reverse (); //Invert element (top row to last, last row to top), return array address
    2. Arrayobj.sort (); //Sort array elements, return the arrays address





8. String of array elements

[JavaScript]View Plaincopy
    1. Arrayobj.join (separator);  //Returns a string that connects each element value of an array together, separated by separator.
    2. toLocaleString, toString, valueOf: Can be seen as a special use of joins, not commonly used





Ii. 3 properties of an array object


1. Length Property


The Length property represents the size of the array, which is the number of elements in it. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1. Unlike most other languages, the length property of a JavaScript array is variable, which requires special attention. When the length property is set larger, the state of the entire array does not change in fact, only the length property becomes larger, and when the length property is set to an earlier hour, the value of the element whose index is greater than or equal to length in the original array is lost. Here is an example that shows changing the length property:

[JavaScript]View Plaincopy
  1. var arr=[12,23,5,3,25,98,76,54,56,76];
  2. Defines an array that contains 10 numbers
  3. alert (arr.length); //Display the length of the array
  4. arr.length=12; //Increase the length of the array
  5. alert (arr.length); //display array length has changed to
  6. Alert (arr[8]); //Displays the value of the 9th element as a
  7. arr.length=5; //Reduce the length of the array to 5, the elements with an index equal to or more than 5 are discarded
  8. Alert (arr[8]); //show 9th element has changed to "undefined"
  9. arr.length=10; //Restore the array length to ten
  10. Alert (arr[8]); //Although the length is restored to 10, the 9th element cannot be retracted, showing "undefined"





By the above code we can clearly see the property of the length property. But the length object can be not only explicitly set, it may also be implicitly modified. You can use an undeclared variable in JavaScript, or you can use an undefined array element (an element that has an index that exceeds or equals length), at which point the value of the length property is set to the value of the index of the element being used plus 1. For example, the following code:

[JavaScript]View Plaincopy
    1. var arr=[12,23,5,3,25,98,76,54,56,76];
    2. alert (arr.length);
    3. arr[15]=34;
    4. alert (arr.length);





The code also defines a 10-digit array, with the alert statement to see its length as 10. An element with an index of 15 is then assigned a value of 15, or arr[15]=34, and then the length of the array is output with the alert statement, resulting in 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, the initial length of an array created with the new Array () is 0, and it is the operation that does not define the element in it that changes the length of the array.


As you can see from the above introduction, the length property is so magical that it makes it easy to increase or decrease the capacity of the array. Therefore, an in-depth understanding of the length attribute can be used flexibly in the development process.


2. Prototype Properties


Returns a reference to the object type prototype. The prototype property is common to object.

[JavaScript]View Plaincopy
    1. Objectname.prototype
    2. The ObjectName parameter is the name of the object.





Description: Provides a basic set of functions for a class of objects using the prototype property. The new instance of the object "inherits" the action given to the object's prototype.


For array objects, use the following example to illustrate the purpose of the prototype property.


Adds a method to the array object that returns the value of the largest element in the array. To do this, declare a function, add it to array.prototype, and use it.

[JavaScript]View Plaincopy
  1. function Array_max ()
  2. {
  3. var i, max = this[0];
  4. For (i = 1; i < this.length; i++)
  5. {
  6. if (Max < this[i])
  7. max = This[i];
  8. }
  9. return Max;
  10. }
  11. Array.prototype.max = Array_max;
  12. var x = New Array (1, 2, 3, 4, 5, 6);
  13. var y = X.max ();





After the code executes, Y saves the maximum value in the array x, or 6.


3. Constructor properties


Represents a function that creates an object.

[JavaScript]View Plaincopy
    1. Object.constructor //object is the name of the object or function.





Description: The constructor property is a member of all objects that have prototype. They include all JScript intrinsic objects except the Global and Math objects. The constructor property holds a reference to a function that constructs a particular object instance.


For example:

[JavaScript]View Plaincopy
    1. x = New String ("Hi");
    2. if (X.constructor = = String) //Processing (condition is true).
    3. Or
    4. function MyFunc {
    5. function body.
    6. }
    7. y = new MyFunc;
    8. if (Y.constructor = = MyFunc) //Processing (condition is true).

JavaScript Learning (one) array operations

Related Article

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.