About the array in JS

Source: Internet
Author: User

arrays, strings, and numbers are the most basic components of a program, and for programmers, understanding them is just the basic operation, and today we're talking about arrays

Unlike other strongly typed languages, arrays can hold any type of value, such as strings, numbers, objects, or other arrays (where multidimensional arrays are used), and JS arrays can be arbitrarily extended as objects (in fact arrays are objects)

It does not require a preset length, as in other strongly typed languages.

How to declare an array :

1, var a=[1,2, ' B ']

2, var a = new Array (3);//Can be automatically mended without new, without new

A[0]=1

a[1]=2;

A[2]= ' B '

3. var a = new Array (+, ' B ')

The most commonly used of course is the first kind.

Sparse array: sparse array is the array containing the vacant units, when used to pay special attention, the vacancy unit by default is defined as undefined, sometimes there may be unexpected consequences, such as different browsers on the processing of empty cells is not the same

In addition, you can try the following code in different browsers results

var a =[]; a.length=3; Console.log (a)

var b=new Array (3); Console.log (b);

var c=[undefined,undefined,undefined]; Console.log (c);

In short, try not to use.

There is another interesting place ; arrays are also objects, so they can also contain key values and attributes, but note that these are not counted in length

var a = []

a[0]=0; a[' text ']= ' Hello ';

Console.log (A.length)//1

A.text//Hello

a[' text ']//hello

In addition, if a key value can be coerced into a string, he will be treated as a numeric index.

This is interesting but try not to use this method in the array, this is what the object does, and the array is used to hold the elements.

Array Method :

(1) Join () method: receives a parameter, which is a string used as a delimiter, and then returns a string containing all the array items
(2) Push () method: Receive any number of parameters, add them to the end of the array one by one, and return the length of the modified array
(3) Pop () method: Removes the last item from the end of the array, reduces the length value of the array, and then returns the removed item
(4) Shift () method: Move the first item in the array and return the change, minus 1
(5) Unshift () method: Add any item to the front of the array and return the length of the new array
(6) Sort () method: Sort data in an array
(7) Concat () method: You can create a new array based on all the items in the current array
(8) Slice () method: Creates a new array based on one or more items in the current array
(9) IndexOf () method: Look backwards from the beginning of the array (position 0)
() lastIndexOf (): Looking forward from the end of the array
(one) Splice () method: I am afraid to be the most powerful array method, it has many kinds of usage, splice () is the main purpose is to insert the middle of the array of items, but the way to use this method has the following 3 kinds:

Delete: You can delete any number of items by specifying only 2 parameters (the position of the first item to be deleted and the number of items to be deleted), for example: Splice (0,2) deletes the first two items in the array

Insert: You can insert any number of items to the specified location, providing only 3 parameters (starting position, 0 (number of items to delete), and items to insert, for example: Splice (2,0, "Red", "green") inserts the string "Red" and "green" from position 2 of the current array

    Replace: You can insert any number of items at the specified location and delete any number of items at the same time, providing only 3 parameters (starting position, number of items to delete, and any number of items to insert), for example: Splice (2,1, "Red", "green") deletes the current array bit 2, and then insert the string "red" and "green" starting at position 2;

Specific can refer to the https://www.cnblogs.com/ToNi/p/4267463.html

class Array :

an array of classes, that is, not a real array, but very similar, such as the nodes returned by the DOM query, such as the arguments object that holds the parameters of the function (ES6 abolished)

These class arrays can access the elements by means of subscripts, but there are no many methods of arrays, which can be transformed using an array method

Commonly used is the slice (...)

function foo () {

var arr = Array.prototype.slice.call (arguments);

Arr.push (' abc ')

Console.log (arr)

}

Foo (' Baz ', ' bar ')//[' baz ', ' bar ', ' abc ']

ES6 built-in function array.from (...) can also achieve the same function;

About the array in JS

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.