JavaScript Array Manipulation Learning notes

Source: Internet
Author: User
Tags array length join javascript array

1, the creation of the array

The code is as follows Copy Code

var arrayobj = new Array (); Create an array

var arrayobj = new Array ([size]); Create an array and specify the length, note not the upper limit, is the length

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, in all cases the array is longer, that is, even if you specify a length of 5, you can still store the elements outside the specified length, note: the length changes.

2, access to elements of the array

The code is as follows Copy Code

var testgetarrvalue=arrayobj[1]; Gets the element value of an array

Arrayobj[1]= "This is the new value"; Give a new value to an array element

3, the addition of array elements

The code is as follows Copy Code

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]]]); /Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned

Arrayobj.splice (insertpos,0,[item1[, item2[, ...) [, Itemn]]]); /inserts one or more new elements into the array at the specified position, and the element at the insertion point is automatically moved back to "".

4, the deletion of the elements of the array

Arrayobj.pop (); Removes the last element and returns the element value

Arrayobj.shift (); Removes the first element and returns the element value, and the elements in the array are automatically moved forward

Arrayobj.splice (Deletepos,deletecount); Deletes the specified number of DeleteCount elements starting at the specified position, deletepos the removed elements

Shift: Deletes the first item of the original array and returns the value of the deleted element, or returns undefined if the array is empty

The code is as follows Copy Code

var a = [1,2,3,4,5];
var B = A.shift (); a:[2,3,4,5] B:1

Unshift: Adds a parameter to the beginning of the original array and returns the length of the array

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = A.unshift ( -2,-1); a:[-2,-1,1,2,3,4,5] B:7

Note: The test return value under IE6.0 is always undefined,ff2.0 the test return value is 7, so the return value of this method is unreliable, and it is necessary to use splice instead of this method with the return value.

Pop: Deletes the last item of the original array and returns the value of the deleted element, or returns undefined if the array is empty

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = A.pop (); a:[1,2,3,4] B:5


5, the array of interception and merging

Concat: Returns a new array that adds a parameter to the original array.

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = A.concat (6,7); a:[1,2,3,4,5] b:[1,2,3,4,5,6,7]

Slice () is used to intercept a portion of an array, which I use to copy the array, which is formatted as follows:
Array.slice (start, end)
If the end argument is omitted, the Shard array contains all the elements starting at start and ending with the array.

6, the copy of the array

Arrayobj.slice (0); Returns an array of copies, noting that a new array is not a pointer to the

Arrayobj.concat (); Returns an array of copies, noting that a new array is not a pointer to the

7, the ordering of array elements

Reverse: Reverse-order the array

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = A.reverse (); a:[5,4,3,2,1] b:[5,4,3,2,1]

Sort (orderfunction): Sorting by specified array of parameters

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = A.sort (); a:[1,2,3,4,5] b:[1,2,3,4,5]

Slice (Start,end): Returns a new array of items from the original array specifying the start subscript to the end subscript

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = a.slice (2,5); a:[1,2,3,4,5] b:[3,4,5]

Join (Separator): Sets the element of an array to a string, separator as a delimiter, and omitted by default with a comma as the delimiter

The code is as follows Copy Code
var a = [1,2,3,4,5];
var B = A.join ("|"); a:[1,2,3,4,5] B: "1|2|3|4|5"

8. String of array elements

Arrayobj.join (separator); Returns a string that connects each element value of an array, separated by a separator in the middle.

toLocaleString, toString, valueof: Can be seen as a special use of join, not commonly used

Two, 3 properties of an array object

1, Length Property

The

    Length property represents the length of the array, which is the number of elements. Because the index of an array always starts with 0, the upper and lower bounds of an array are: 0 and length-1 respectively. 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 actually change, only the length property is larger, and when the length property is set to the previous hour, the value of the element whose index is greater than or equal to length in the original array is lost. The following is an example of changing the length property:

  code is as follows copy code

var arr=[ 12,23,5,3,25,98,76,54,56,76];

//Defines an array that contains 10 digits

Alert (arr.length);//show length of array

Arr.length=12;//Increase the length of the array

Alert (arr.length);//show the length of the array has changed to

Alert (arr[8]);//Display the value of the 9th element, which is

Arr.length=5//Reduce the length of the array to 5, and the elements indexed equal to or more than 5 are discarded

Alert (arr[8]);//show 9th element has become "undefined"

arr.length=10;//restore array length to ten

Alert (arr[8]);//Although the length is restored to 10, the 9th element cannot be retracted, displaying the "undefined"

   

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

The code is as follows Copy 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 first defines an array of 10 digits, which can be seen by an alert statement of 10. Then the element with index 15 is assigned to 15, or arr[15]=34, and then the length of the array is output by the alert statement, with 16. In any case, this is a surprising feature for developers who are accustomed to strongly typed programming. In fact, an array created with the new Array () has an initial length of 0, and it is an operation that does not define an element in it, which 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 size of the array. Therefore, a thorough understanding of the length attribute is helpful to the flexible application in the development process.

2, prototype property

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

Objectname.prototype

The objectname parameter is the name of the object.

Description: Provides a set of basic functions for an object's class with the prototype property. The operation of the new instance of the object, "inherit", gives the object a prototype.

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

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

The code is as follows Copy Code

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 the code executes, Y saves the maximum value in the array x, or says 6.

3, constructor property

Represents a function that creates an object.

Object.constructor//object is the name of an object or function.

Description: The constructor property is a member of all objects that have prototype. They include all of the 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:

The code is as follows Copy Code

x = new String ("Hi");

if (X.constructor = = String)//To be processed (condition is true).

Or

function MyFunc {

function body.

}

y = new MyFunc;

if (Y.constructor = = MyFunc)//To be processed (condition is true).

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.