A summary of javascript learning every day (Array), javascriptarray

Source: Internet
Author: User

A summary of javascript learning every day (Array), javascriptarray

1. Common array Methods

var colors = ["red", "blue", "green"];  //creates an array with three strings    alert(colors.toString());  //red,blue,green    alert(colors.valueOf());   //red,blue,green    alert(colors);        //red,blue,green

2. array map () method
 

Var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var mapResult = numbers. map (function (item, index, array) {// index element of the item array corresponds to the index of the original array console. log (array = numbers); // true return item * 2;}); console. log (mapResult); // [2, 4, 6, 8, 10, 8, 6, 4, 2]

3. array reduce () method

Var values = [1, 2, 3, 4, 5]; // receives a function and traverses the item from left to right until reduce reaches a value. Var sum = values. reduce (function (prev, cur, index, array) {console. log (array = values); console. log (index); // The index of arrays 1, 2, 3, and 4 start from 1 and return prev + cur; // Add the two values before and after}); alert (sum); // 15

4. array concat () method

// The concat () method is used to connect two or more arrays. // This method does not change the existing array, but only returns a copy of the connected array. // Syntax // arrayObject. concat (arrayX, arrayX ,......, arrayX) var colors = ["red", "green", "blue"]; var colors2 = colors. concat ("yellow", ["black", "brown"]); alert (colors); // red, green, blue alert (colors2); // red, green, blue, yellow, black, brown

5. array length

 var colors = new Array(3);   //create an array with three items    var names = new Array("Greg"); //create an array with one item, the string "Greg"    alert(colors.length);//3    alert(names.length);//1
 var colors = ["red", "blue", "green"]; //creates an array with three strings    var names = [];            //creates an empty array    var values = [1,2,];          //AVOID! Creates an array with 2 or 3 items    var options = [,,,,,];         //AVOID! creates an array with 5 or 6 items        alert(colors.length);  //3    alert(names.length);   //0    alert(values.length);  //2 (FF, Safari, Opera) or 3 (IE)    alert(options.length);  //5 (FF, Safari, Opera) or 6 (IE)
var colors = ["red", "blue", "green"];  //creates an array with three strings    colors.length = 2;    alert(colors[2]);    //undefined
var colors = ["red", "blue", "green"];  //creates an array with three strings    colors.length = 4;    alert(colors[3]);    //undefined
 var colors = ["red", "blue", "green"];  //creates an array with three strings    colors[colors.length] = "black";     //add a color    colors[colors.length] = "brown";     //add another color    alert(colors.length);  //5    alert(colors[3]);    //black    alert(colors[4]);    //brown
var colors = ["red", "blue", "green"];  //creates an array with three strings    colors[99] = "black";           //add a color (position 99)    alert(colors.length); //100

6. array methods every and some

// The every () and some () methods are both Iterative Methods of arrays in JS. // Every () is used to run a given function for each item in the array. If this function returns true for each item, true is returned. // Some () is to run the specified function for each item in the array. If this function returns true for any item, true is returned. Var numbers = [1, 2, 3, 4, 5, 4, 3, 2, 1]; var everyResult = numbers. every (function (item, index, array) {return (item> 2) ;}); alert (everyResult); // false var someResult = numbers. some (function (item, index, array) {return (item> 2) ;}); alert (someResult); // true

7. array filter () method

// Find the element from the array that is suitable for the condition (for example, greater than the value of an element) var numbers = [1, 2, 3, 4, 3, 2, 1]; var filterResult = numbers. filter (function (item, index, array) {return (item> 2) ;}); alert (filterResult); // [3, 4, 5, 3]

8. array indexOf and lastIndexOf

// The indexOf () method returns the position of the first occurrence of a specified string value. // Syntax // stringObject. indexOf (searchvalue, fromindex) // searchvalue is required. Specifies the string value to be retrieved. // Fromindex integer. Specifies the position where the search starts in the string. The valid value is 0 to stringObject. length-1. If this parameter is omitted, It is retrieved from the first character of the string. /* The lastIndexOf () method returns the last position of a specified string value and searches forward from the specified position in the string. The syntax stringObject. lastIndexOf (searchvalue, fromindex) searchvalue is required. Specifies the string value to be retrieved. Fromindex integer. Specifies the position where the search starts in the string. The valid value is 0 to stringObject. length-1. If this parameter is omitted, It is retrieved from the last character of the string. */Var numbers = [1, 2, 3, 4, 4, 3, 2, 1]; alert (numbers. indexOf (4); // 3 alert (numbers. lastIndexOf (4); // 5 alert (numbers. indexOf (4, 4); // 5 alert (numbers. lastIndexOf (4, 4); // 3 var person = {name: "Nicolas"}; var people = [{name: "Nicolas"}]; var morePeople = [person]; alert (people. indexOf (person); //-1 alert (morePeople. indexOf (person); // 0

9. array toLocaleString and toString

 var person1 = {      toLocaleString : function () {        return "Nikolaos";      },            toString : function() {        return "Nicholas";      }    };        var person2 = {      toLocaleString : function () {        return "Grigorios";      },            toString : function() {        return "Greg";      }    };        var people = [person1, person2];    alert(people);           //Nicholas,Greg    alert(people.toString());      //Nicholas,Greg    alert(people.toLocaleString());   //Nikolaos,Grigorios

10. array push and pop Methods

var colors = new Array();           //create an array    var count = colors.push("red", "green");    //push two items    alert(count); //2        count = colors.push("black");         //push another item on    alert(count); //3        var item = colors.pop();            //get the last item    alert(item);  //"black"    alert(colors.length); //2

11. array Methods: unshift and shift

// The unshift () method can add one or more elements to the beginning of the array and return a new length. // Shift () is used to delete the first element of the array from it and return the value of the first element. Var colors = new Array (); // create an array var count = colors. unshift ("red", "green"); // push two items alert (count); // 2 count = colors. unshift ("black"); // push another item on alert (count); // 3 var item = colors. pop (); // get the first item alert (item); // "green" alert (colors. length); // 2

12. array reverse Method reverse

 var values = [1, 2, 3, 4, 5];    values.reverse();    alert(values);    //5,4,3,2,1

13. array sorting method sort

Function compare (value1, value2) {if (value1 <value2) {return-1;} else if (value1> value2) {return 1;} else {return 0 ;}} var values = [0, 1, 16, 10, 15]; values. sort (compare); alert (values); //, 10, 15, 16 // sort changes the original array

14. array method slice

/* The slice () method returns the selected element from an existing array. Syntax: arrayObject. slice (start, end) start is required. Specifies where to start selection. If it is a negative number, it specifies the position starting from the end of the array. That is to say,-1 refers to the last element,-2 refers to the second to last element, and so on. End is optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the split array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the elements starting from the end of the array. The Return Value Returns a new array containing elements in the arrayObject from start to end (excluding this element. */Var colors = ["red", "green", "blue", "yellow", "purple"]; var colors2 = colors. slice (1); var colors3 = colors. slice (); alert (colors2); // green, blue, yellow, purple alert (colors3); // green, blue, yellow

15. array method splice

/* The plice () method adds/deletes a project to/from the array, and then returns the deleted project. Note: This method will change the original array. Syntax: arrayObject. splice (index, howmany, item1,..., itemX) index is required. Integer that specifies the position of the added/deleted project. A negative number can be specified from the end of the array. Howmany is required. The number of projects to delete. If it is set to 0, the project will not be deleted. Item1,..., itemX (optional. Add a new project to the array. */Var colors = ["red", "green", "blue"]; var removed = colors. splice (0, 1); // remove the first item alert (colors); // green, blue alert (removed); // red-one item array removed = colors. splice (1, 0, "yellow", "orange"); // insert two items at position 1 alert (colors); // green, yellow, orange, blue alert (removed); // empty array removed = colors. splice (1, 1, "red", "purple"); // insert two values, remove one alert (colors); // green, red, purple, orange, blue alert (removed); // yellow-one item array

16. array isArray () method

alert(Array.isArray([]));  //true    alert(Array.isArray({}));  //false

The above is the summary of today's javascript learning, and will be updated every day. I hope you will continue to pay attention to it.

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.