JavaScript Advanced Programming (Third edition) Chapter fifth reference types

Source: Internet
Author: User

5.2 Array Type
1 var colors = new Array (3);      Create an array of 3 items with 2 var names = new Array ("Greg");  Create an array that contains 1 items, the string "Greg"
5.2.2 Conversion method
1 var colors = ["Red", "Blue", "green"];    Create an array containing 3 strings of 2alert (colors.tostring ());    Red,blue,green3alert (colors.valueof ());     Red,blue,green4 alert (colors);               Red,blue,green
1 var colors = ["Red", "green", "Blue"]; 2 Alert (Colors.join (","));      Red,green,blue3 alert (colors.join ("| |)");     red| | green| | Blue

5.2.3 Stack method
1 var colors = ["Red", "Blue"]; 2 Colors.push ("Brown");              Add another item 3colors[3] = "BLACK";               Add an item 4alert (colors.length);  45         6var item = Colors.pop ();           Get the last 7 alert (item);  "Black"

5.2.4 Queue method
1 var colors = new Array ();                      Create an array of 2var count = Colors.push ("Red", "green");       Push in two 3alert (count);  24         5count = Colors.push ("Black");                  Push into another 6alert (count);  37         8var item = Colors.shift ();                     Get the first 9 alert (colors.length);  2

5.2.5 re-ordering method
1 var values = [1, 2, 3, 4, 5]; 2 values.reverse (); 3 alert (values);       5,4,3,2,1
1 function Compare (value1, value2) {2if (value1<value2) {3 return 1;4 } else if (value1>value2) {5 return-1;6 } else {7 return 0;8     }9 }Ten  One var values = [0, 1, 5, ten, +]; A Values.sort (compare); -alert (values); 15,10,5,1,0

5.2.6 Method of operation
1 var colors = ["Red", "green", "Blue"]; 2 var colors2 = Colors.concat ("Yellow", ["Black", "Brown"]); 3 4 alert (colors);     Red,green,blue        5 alert (colors2);    Red,green,blue,yellow,black,brown
1 var colors = ["Red", "green", "blue", "yellow", "purple"); 2 var colors2 = colors.slice (1); 3 var colors3 = Colors.slice (1,4); 4         5 alert (colors2);   Green,blue,yellow,purple6 alert (COLORS3);   Green,blue,yellow

1 var colors = ["Red", "green", "Blue"];2 var removed = Colors.splice (0,1); Delete First item3 alert (colors); Green,blue4 alert (removed); Red, the returned array contains only one item5         6 removed = colors.splice (1, 0, "yellow", "orange"); Insert two items starting from position 17 alert (colors); Green,yellow,orange,blue8 alert (removed); An empty array is returned.9 Ten removed = Colors.splice (1, 1, "Red", "purple"); Insert two items to delete an item One alert (colors); Green,red,purple,orange,blue Aalert (removed); Yellow, the returned array contains only one item

5.2.7 Location method
1 var numbers = [1,2,3,4,5,4,3,2,1];2 3 Alert (Numbers.indexof (4)); 34 Alert (Numbers.lastindexof (4)); 55 6 Alert (Numbers.indexof (4, 4)); 57 Alert (Numbers.lastindexof (4, 4));//38 var person = {Name: "Nicholas"};9 var people = [{name: ' Nicholas '}];Ten var morepeople = [person]; One  A Alert (People.indexof (person)); -1 -Alert (Morepeople.indexof (person)); 0

5.2.8 Iterative method
    • Every (): Runs the given function for each item in the array, and returns True if the function returns true for each item.
    • Filter (): Each item in an array runs the given function, and returns a list of items that are true of the function.
    • ForEach (): Runs the given function for each item in the array. This method has no return value.
    • Map (): Each item in the array is a given function that returns an array of the results of each function call.
    • Some (): Runs the given function for each item in the array, and returns True if the function returns true for either item.
1 var numbers = [1,2,3,4,5,4,3,2,1];2         3 var everyresult = numbers.every (function (item, index, array) {4 return (item > 2);5     });6         7 alert (Everyresult); False8         9 var someresult = numbers.some (function (item, index, array) {Ten return (item > 2); One     }); A          -alert (Someresult); True
 1  var numbers = [1,2,3,4,5,4,3,2,1];  2  var F Ilterresult = Numbers.filter (function (item, index, array) { 4   return (item > 2);  5  });    6  7  alert (filterresult); [3,4,5,4,3] 
1         var numbers = [1,2,3,4,5,4,3,2,1]; 2         3         var mapresult = numbers.map (function (item, index, array) {4            return item * 2; 5         }); 6         7         alert (mapresult);   [2,4,6,8,10,8,6,4,2]

JavaScript Advanced Programming (Third edition) Chapter fifth reference types

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.