JavaScript Learning Path (2)--arrays

Source: Internet
Author: User
Tags javascript array

The Array type is probably the most common type in JavaScript. Also, arrays in JavaScript are quite different from arrays in most other languages. Although JavaScript arrays and arrays in other languages are an ordered list of data, unlike other languages, each item of a JavaScript array can hold any type of data. That is, you can use the first position of the array to hold the string, save the value with a second position, save the object in a third position, and so on. Furthermore, the size of the JavaScript array can be dynamically adjusted, i.e. it can grow automatically as the data is added to accommodate the new data.
1, arrays are created in the following ways:

var colors = new Array (), var colors = new Array (20),//Create an array of length 20 var colors = new Array ("Red", "Blue", "green");//Create a 3-dollar The array var colors = array (3); When using the array constructor, you can also omit the new operator var names = [];//Create an empty array

2, adding elements to the array

Method One: Add var names=new array () using the index, names[0]= "Android"; names[1]= "PHP"; names[2]= "JAVA"; mode two: Stack method add push always inserts elements into the end of the array Var Names=new Array (); Names.push ("Android"); Names.push ("PHP"); Names.push ("JAVA");
Method Three: Queue method Add unshift always add elements to the array's front end var names=new array (), Names.push ("Android"), Names.unshift ("Java"), Names.unshift (" PHP ");

  

3, deleting array elements

Way one: Remove the trailing element var names=["Android", "Java", "PHP"];names.pop (); Result: Android,java mode two: Delete an element 1, you can first define a function for JS array object, Used to find the position of the specified element in the array, i.e. index: Array.prototype.indexOf = function (val) {for        (var i = 0; i < this.length; i++) {            if (the This [I] = = val) return i;        }        return-1;    }; 2, then use the JS array's own intrinsic function to delete this element by getting the index of this element: Array.prototype.remove = function (val) {        var index = This.indexof (val);        if (Index >-1) {            This.splice (index, 1);        }    }; 3, method call: Var names= [' Android ', ' JAVA ', ' PHP '];names.remove (' Android ');] Results: java,php

4, modifying array elements

Method One: Modify the array elements by index var names = [' Android ', ' Java ', ' PHP '];names2[1]= ' java '; Result: android,java,php mode two: Modify 1 by element value to get the index of the element in the array  Array.prototype.remove = function (val) {        var index = This.indexof (val);        if (Index >-1) {            This.splice (index, 1);        }    }; 2, add modify element method to array: Array.prototype.change = function (oldvalue,newvalue) {        var index = this.indexof (oldValue);        if (Index >-1) {           this[index]=newvalue;        }    }; 3, method call: var names = [' Android ', ' JAVA ', ' PHP '];names.change (' Android ', ' Android '); Result: android,java,php

5, gets the array element value

Way one: Get the value by index var names = [' Android ', ' JAVA ', ' PHP '];var value=names[0]; Result: Android mode two: Get the first value of the array element: var names = [' Android ', ' JAVA ', ' PHP '];var value=names.shift (); Results: Android

JavaScript Learning Path (2)--arrays

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.