Record Array manipulation in JavaScript

Source: Internet
Author: User

1. means of definition;
var arr = []; Array literal
var arr = new Array (); Array constructors

Array all methods are derived from Array.prototype

2. Reading and writing of arrays;
Arr[num]//Can not overflow read results undefined
Arr[num] = xxx; Can overflow write

3. Change the original array
Push ()
Add data to the last digit of the array, the return value is the length of the array;
To simulate an array method:
Array.prototype.push = function () {
for (Var i=0;i<arguments.lenght;i++) {
This[this.length] = arguments[i];
}
return this.length;
}

Pop ()
The last digit of the cut array, the return value is the current cut value;

Shift ()
Cut the first bit of the array, the return value is the current cut value;

Unshift ()
Add data at the top of the array, and the return value is the length of the array;

Sort (FN)
Sort
1. Two parameters must be written
2. See return value 1) when the return value is negative, the preceding number is placed in front
2) is a positive number, and the following numbers are placed in front
3) is 0, do not move
Arr.sort (function (A, b) {
return a-B;
});

Reverse ()
Reverse (Reversed) Order

Splice (starting from the first, intercept the length, add new data at the incision)
Can be added, deleted, changed. The return value is a clipped value

4. Do not change the original array
Concat ()
Link array arr.concat (ARR1); The return value is a new array;

Join ()
The argument must be a string, convert the array to a string, and link to the parameter.

ToString ()
Convert an array to a string

Slice (intercept from this bit, intercept to that bit);
Intercept


# # # class Array
1. property to be an indexed (numeric) attribute, must have length
2. Does not have the method that the array has
You can add array methods to an array of classes, such as Push:Array.prototype.push
If you add the splice method, it will be the same as the number leader [1, 2, 3]

var obj = {
"0": ' A ',
"1": ' B ',
"2 ': ' C ',
"Length": 3,
"Push": Array.prototype.push,
"Splice": Array.prototype.splice
}

Benefit: (class array push is replaced by length)
Array.prototype.push = function (target) {
Obj[obj.length] = target;
Obj.length + +;
}

Record Array manipulation in JavaScript

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.