JS array-related knowledge

Source: Internet
Author: User

To create an array method one:

            var a1=New Array (5);            Console.log (a1.length); // 5            // [], the array is empty             var a2=new Array (5,6);            Console.log (a2.length); // 2            // [5,6]            

Create array Two:

           var a1=[5];            Console.log (a1.length); // 1            // [5]             var A2=[5,6];            Console.log (a2.length); // 2            // [5,6]    

Index and length of the array:

var a1=[1,2,3,4];console.log (a1[//1var i=1//2// 3

Console.log (1 in A1); // true, is indeed a property

           var a=New Array (+/-);            a[100]=100;             // 101            // undefined            // undefined            Console.log (a[100]); 100

In the example above:

Although direct to a[100] assignment does not affect a[4] or a[99], but the length of the array is affected, the array is equal to the maximum number of index+1 in the array, we know the length property of the array is also
is a writable property that, when forced to set the length property value of an array to be less than or equal to the maximum index value, the array automatically deletes data with indexd greater than or equal to length, appending a few sentences in the code just now
a.length=2Console.log (a); // [up]
A.length=5/ /// back No 3 undefined

Element additions/Deletions

            var a=New Array (+/-);            a[3]=4;            Console.log (a); // [1, 2, 3, 4]
           Delete a[2];            Console.log (a[//undefined

JS Stack method:

            var a=New Array (+/-);            A.push (4);            Console.log (a); //             Console.log (a.length);   4            Console.log (A.pop (a));   4            //            console.log (a.length);   3

The queue method of JS

            var a=New Array (+/-);            A.unshift (4);            Console.log (a); //             Console.log (a.length);   4            Console.log (A.shift (a));   4            //            console.log (a.length);   3

How to delete JS

            var a=New Array (1,2,3,4,5);            Console.log (A.splice (1,3)); //             Console.log (a.length);   2            Console.log (a);   [1,5]

The JS loop iterates through the array:

New Array (13.5,3,4,5,6);  for (Var i=0;i<arr.length;i++) {  alert (arr[i]);}
New Array () mycars[0] = "Saab"mycars[1] = "Volvo"mycars[2] = "BMW"for + "< br/> ")}

The conversion between JS array and string:

New Array (0,1,2,3,4= A.join ("-");

var s = "Abc,abcd,aaa"= S.split (","); // decompose at each comma (,). 

JS array-related knowledge

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.