JavaScript Array Learning

Source: Internet
Author: User

Record the process of learning an array 1, creating an array
1 var New Array (); // Empty Array 2 var ary2= []; // literal quantity
2. Array detection
// Method One if instanceof Array) {}// method Two ES5if(Array.isarray (Array)) {}//  Object.prototype.toString.apply (arrary,arguments);
3. Array conversion method
//1, toString ();        varary =[' aaaa ', ' VVVVV ']; Ary.tostring ();//"AAAA,VVVVV"//2, ValueOf ();    varary =[' green ', ' blue ', ' red ']; Ary.valueof ();//["Green", "Blue", "Red"]//3, toLocaleString ();Ary.tolocalestring ();//"Green,blue,red"//4, join ();Ary.join (' @@@&&&&&***** ')//"[Email protected]@@&&&&&*****[email protected]@@&&&&&*****red"
4. Find a Way

' Last in first out ' performance

ary =[' AA ', ' BBB ', ' cccc ', ' dddd ']; // ["AA", "BBB", "CCCC", "dddd"] Ary.pop (); // "dddd"ary.push (' dddd '); // 4 ary // ["AA", "BBB", "CCCC", "dddd"]
5. Queue method

The performance of ' FIFO '

var ary = ["AA", "BBB", "CCCC", "dddd"];ary.shift (); // "AA"ary.unshift (' aaaaaaaaaaaa '); // 4console.log (ary); // ["Aaaaaaaaaaaa", "BBB", "CCCC", "dddd"]
6. Loop Array (scroll selection)
1, from the back to the front
var ary =[10,20,30,40,50,60,70,80,90]; // from the back to the front // Ary.unshift (Ary.pop ());  for (var i = 0, Len =ary.length; i<len;i++) {    console.log (ary);    Ary.unshift (Ary.pop ());    Console.log (ary);}

2. From the front to the back

 for (var i = 0, Len =ary.length; i<len;i++) {    console.log (ary);    Ary.push (Ary.shift ());    Console.log (ary);}
7. Array sorting
var ary =[90,100,90,33300,10,20,30,40,50,60,70,80,90];ary.sort (function(val1,val2 ) {    return val1-val2;}); // [Ten, +, +,--]----------------33300] Ary.sort (function(val1,val2)    {
   
    return val2-
    val1;}); 
    // 
    [33300, +,------------
   
8. Array method

1, concat ();

Ary.concat (' AAA ', [' ddd ', ' CCC ', ' eeee '); // [33300, +,------------------------

2, slice ();

var ary1 = [33300, +,-------------------------- /c4>0,5); // [33300,N, Console.log (ary1); // [33300,------------------------------- // [+]

3, splice (); The most powerful method

varAry2 = [' A ', ' B ', ' C ', ' d ', ' e ', ' AA ', ' BB ', ' CC ']ary2//["A", "B", "C", "D", "E", "AA", "BB", "CC"]Ary2.splice (0,0);//[]Ary2//["A", "B", "C", "D", "E", "AA", "BB", "CC"]Ary2.splice (1,3);//["B", "C", "D"]Ary2//["A", "E", "AA", "BB", "CC"]Ary2.splice (0,0, ' a ', ' aaaa '));//[]Ary2//["A", "AAAA", "a", "E", "AA", "BB", "CC"]
9. Position method (ES5)
Ary1 = [' 222 ', ' ffff ', ' sfff ', ' a ']//["222", "FFFF", "Sfff", "a"]ary1.indexof (' a '); // 3ary1.indexof (' aaaaa '); // -1
10. Array method in ES5 (iterative method)

1.every (); Returns true for all values in the array

2.some (); Returns true if the array

3.forEach (); the array each run function, no return value

4.map (); Every item in an array runs a function that returns an array

5.filter (); An array of each run function that returns an array of ture

varAry1 = [10,20,5,44,444];ary1.some (function(value,index) {returnvalue>100;});//tureAry1.every (function(value) {returnValue<0;});//falseAry1.map (function(value,index) {returnValue+index;});//[Ten, 7, 448]Ary1.filter (function(value,index) {returnValue>40;});//[444]Ary1.foreach (function(Value,index) {Ary1[index]=value*2;}); Console.log (ary1);//[+, 888]

JavaScript Array Learning

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.