JavaScript array Operations

Source: Internet
Author: User
Tags javascript array
4.1, create

var arrayobj = new Array ();
var arrayobj = new Array ([size]);
var arrayobj = new Array ([element0[, element1[, ...) [, ELEMENTN]]]);

Example:

            var array11 = new Array ();  Empty array
            var array12 = new Array (5);  Specifies the length that can be crossed by
            var array13 = new Array ("A", "B", "C", 1,2,3,true,false);  Define and assign value
            var array14=[];  Empty array, syntactic sugar
            var array15=[1,2,3, "X", "Y"];  Define and assign values
4.2, Access and modification

var testgetarrvalue=arrayobj[1];

arrayobj[1]= "Value";

            4.2, visit and modify
            array12[8]= "Hello array12";  Assign value or modify
            console.log (Array12[8]);   Value
            //Traversal for
            (var i = 0; i < array13.length; i++) {
                console.log ("arrayl3[" +i+ "]=" +array13[i));
            //enumeration for
            (var i in array15) { 
                Console.log (i+ "=" +array15[i]);  I here is the subscript
            }

Results:

4.3. Add elements

Adds one or more new elements to the end of the array and returns the new length of the array

Arrayobj. Push ([Item1 [item2 [...] [Itemn]]]);

Adds one or more new elements to the beginning of the array, the elements in the array are automatically moved back, and the new length of the array is returned

Arrayobj.unshift ([Item1 [item2 [...] [Itemn]]]);

Inserts one or more new elements into the array at the specified position, automatically moves the elements of the insertion position back, returns an array of deleted elements, deletecount the number of elements to be deleted

Arrayobj.splice (insertpos,deletecount,[item1[, item2[, ...) [, Itemn]]]

Sample code:

            4.3, add element
            var array31=[5,8];
            Add to End
            Array31.push (9);
            var len=array31.push (10,11);
            Console.log ("Length is:" +len+ "-" +array31);
            Add to start
            array31.unshift (4);
            var len=array31.unshift (1,2,3);
            Console.log ("Length is:" +len+ "-" +array31);
            Add to Intermediate
            var len=array31.splice (5,1,6,7);  Inserts from the 5th bit, deletes the 1 elements after the 5th digit, returns the deleted element
            Console.log ("Deleted:" +len+ "-" +array31);

Run Result:

4.4, delete

Removes the last element and returns the element value

Arrayobj.pop ();

Removes the first element and returns the element value, and the elements in the array are automatically moved forward

Arrayobj.shift ();

Deletes the specified number of DeleteCount elements starting at the specified position, deletepos the removed elements

Arrayobj.splice (Deletepos,deletecount);

Example:

            4.4, delete
            var array41=[1,2,3,4,5,6,7,8];
            Console.log ("array41:" +array41);
            Deletes the last element and returns the
            var e=array41.pop ();
            Console.log ("Removed:" +e+ "-" +array41);
            Deletes the header element and returns the
            var e=array41.shift ();
            Console.log ("Removed:" +e+ "-" +array41);
            Deletes the specified location and number of
            var e=array41.splice (1,4);  Deletes 4 console.log from index 1
            ("Deleted: +e+"--"+array41");

Results:

4.5. Interception and Merging

Returns a portion of an array as an array, noting that the end-corresponding element is not included, and if omitting the end copies all elements after start

Arrayobj.slice (start, [end]);

Concatenate multiple arrays (or a string, or a mixture of arrays and strings) into an array, returning a new array of connections

Arrayobj.concat ([item1[, item2[, ...) [, Itemn]]]);

Example:

            4.5. Interception and merging of
            var array51=[1,2,3,4,5,6];
            var array52=[7,8,9,0, "A", "B", "C"];
            Intercept, Slice
            var array53=array51.slice (2);  Intercept from the 3rd element to the last
            Console.log ("intercepted: +array53+"-"+array51");
            var array54=array51.slice (1,4);  Intercept the element
            Console.log ("intercepted: +array54+"-"+array51") with index number 3 starting from the 3rd element;
            Combine
            var array55=array51.concat (array52,["D", "E"], "F", "G");
            Console.log ("After merging:" +array55);

Results:

4.6. Copy

Returns an array of copies, noting that a new array is not a pointer to the

Arrayobj.slice (0);

Returns an array of copies, noting that a new array is not a pointer to the

Arrayobj.concat ();

because the array is a reference data type, the direct assignment does not reach the true implementation copy, the address reference, we need is a deep copy. 4.7, sorting

Reverse element (top to last, last to top), return array address

Arrayobj.reverse ();

array element Sorting, returning the arrays address

Arrayobj.sort ();

Arrayobj.sort (function (obj1,obj2) {});

Example:

            var array71=[4,5,6,1,2,3];
            Array71.sort ();
            Console.log ("Sort after:" +array71);
            var array72=[{name: "Tom", Age:19},{name: "Jack", Age:20},{name: "Lucy", Age:18}];
            Array72.sort (function (user1,user2) {return
                user1.age<user2.age;
            });
            Console.log ("after sort:");
            for (var i in array72) Console.log (array72[i].name+ "," +array72[i].age);

Results:

4.8. Combine into characters

Returns a string that connects each element value of an array, separated by a separator in the middle.

Arrayobj.join (separator);

Sample code:

            4.8. Combine into characters and split characters into array
            var array81=[1,3,5,7,9];
            var ids=array81.join (",");
            Console.log (IDs);
            
            Split into array
            var text= "Hello Nodejs and angular";
            var array82=text.split ("");
            Console.log (array82);

Run Result:

All code:

<! DOCTYPE html> 
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.