An array of JS foundations

Source: Internet
Author: User
Tags array length

Arrays: Arrays are values that use separate variable names to store a series of identical data types.

One-dimensional array: ① empty array: var variable name = new Array ();/var variable name = ""; var variable name =new Array (array length);

② has an array of values: var variable name =new Array ("1" "2");/var variable name = "Three-in-one";

Attention:
var arr1 = [3]; Arr1.length = = 1, arr1[0] = = 3
var arr2 = new Array (3); Arr2.length = = 3, arr2[0] = = undefined

Multidimensional array: var twoarray = new Array ();
TWOARRAY[0] = new Array ();//array index=0 and new array
TWOARRAY[1] = new Array ();//array index=1 and new array

Build Twoarray[0] Array
Twoarray[0][0] = "AA";
TWOARRAY[0][1] = "ba";
TWOARRAY[0][3] = "Ca";
Build Twoarray[1] Array
Twoarray[1][0] = "ACCA";
TWOARRAY[1][1] = "Bada";
TWOARRAY[1][3] = "Cdasa";

var twoarray = [["A0b0", "a0b1"],["a1b0", "A1b1"],["a2b0", "a2b1"]];

var twoarray = new Array ();
for (var i = 0; i < FirstIndex; i++) {
Twoarray[i] = new Array ();
for (var j = 0; J < LastIndex; J + +) {
TWOARRAY[I][J] = "Database BlaBla";
}
}

To iterate over an array:

For loop

var New Array ("1", "2", "3");     for                    (var i = 0;i<aa.length;i++) {       alert (aa[i]);   }


For-in
var New Array ("A", "B", "C");     for                         (var in aa) {       alert (aa[b]);   }

   //Add a value to the array
var arr=new Array (1,2,3,4,5);
var len=arr.push (7,9);
Console.log (Len,arr);//array.push () adds a value at the end of the array, returning the added array length
var arr=new Array (1,2,3,4,5);
var len=arr.unshift (7,9);
Console.log (Len,arr);//array.unshift () adds a value at the beginning of the array, returning the added array length, as opposed to Array.push ()

//Delete the values in the array
var arr=new Array (1,2,3,4,5);
var val=arr.pop ();
Console.log (Val,arr);//delete the value from the end of the array to return the deleted value
var arr=new Array (1,2,3,4,5);
var val=arr.shift ();
Console.log (Val,arr);//delete the value from the beginning of the array returns the deleted value in contrast to the pop ()

//Array conversion string
var arr=new Array (1,2,3,4,5);
var str=arr.join ('-');//interval string, default to ","
Console.log (str);//1-2-3-4-5

//Array sorting
var arr=new Array (1,2,3,4,5);
arr.reverse ();//Reverse Sort
Console.log (arr)//[5, 4, 3, 2, 1]
var arr=new Array (1,2,3,4,5,59,6);
Arr.sort ();
Console.log (arr);//Output [1, 2, 3, 4, 5, 59, 6] Note: sort () By default, the conversion string is reordered so it is not sorted correctly by number size
//correct method to write comparison function
Arr.sort (function (A, b) {return-A-B});//Ascending
Console.log (arr);//Output [1, 2, 3, 4, 5, 6, +]
Arr.sort (function (A, b) {return b-a});//Descending
Console.log (arr);//Output [6, 5, 4, 3, 2, 1]

built-in operation functions for arrays:

//connection array
var arr1=[1,2,3];
var arr2=[4,5,6];
var arr3=[7,8,9];
var arr=arr1.concat (arr2,arr3,[10,11]);
Console.log (arr);//Output [1, 2, 3, 4, 5, 6, 7, 8, 9, ten, one]

returns the selected element in a//existing array
var arr=new Array (1,2,3,4,5);
var arr2=arr.slice (2);
The first parameter start selects the index subscript value 2 for the third value (0,1,2) Number 3 begins with an optional default to the end of the array
Console.log (ARR2);//Output [3, 4, 5]
//Note the end argument is that the parameter is the array at the end of the array fragment subscript 4 is the number 5 of the subscript after intercept 5, can also be truncated to end-1
var arr2=arr.slice (2,4);
Console.log (ARR2);//Output [3, 4] not [3,4,5]
//intercept starts with a negative number
var arr2=arr.slice ( -2,4);
//If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.
///You can also convert an array length (5) plus a negative value (-2) equivalent to Arr.slice (3,4);
Console.log (ARR2);//Output [4]

//splice () implement delete insert Replace array
//Delete
var arr=new Array (1,2,3,4,5);
var arr2=arr.splice;//delete starts with a value of 1 (2) begins with 2 values (2 and 3) to return the deleted value
Console.log (ARR,ARR2);//Output [1, 4, 5] [2, 3]
//Insert
var arr=new Array (1,2,3,4,5);
var arr2=arr.splice (1,0,11,111);//The value is inserted before the value of subscript 1 (2), the second of the parameter is 0 not deleted, insert 11,111
Console.log (ARR,ARR2);//[1, One, 111, 2, 3, 4, 5] [] arr2 returned as an empty array because it is not deleted
//Replace
var arr=new Array (1,2,3,4,5);
var arr2=arr.splice (1,2,11,111);//Replace the value before the value (2) of the subscript 1, the second of the parameter is 2 to remove 2 values, and then insert the 11,111
Console.log (ARR,ARR2);//[1, 11, 111, 4, 5] [2, 3]//delete and add

//Find the subscript where the value is located
var arr=new Array (1,2,3,4,5,6,7,3);
var index=arr.indexof (3);
Console.log (index);//returns 2 detects where the first 3 appears
var index=arr.indexof (3,4);//The second argument starts with a value
Console.log (index);//return 7 detect the position where the first 3 appears, starting with 4 (number 5)
var index=arr.indexof (ten);
Console.log (index);//returns-1 for lookup to return-1
start looking for LastIndexOf () at the end of//
var index=arr.lastindexof (3);
Console.log (index);//Return 7
//indexof () lastIndexOf () Low version browser incompatible

An array of JS foundations

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.