A concise summary of the operation method of JS Common array _ Basic knowledge

Source: Internet
Author: User
Tags array sort
Array segmentation in javascript var colors = ["Red", "green", "blue"];
Alert (colors.tostring ()); Alert (Colors.join ("|"));
The return result is red|green|blue var colors = ["Red", "green", "blue", null]; Alert (Colors.join ("|"));
/red|green|blue| Note that when the value in the array is null or undefined, the result returned is an empty string representing-------------------------------------------//array deletion and adding var colors = ["
Red "," green "," blue "];
Alert (colors.tostring ()); Colors.push ("White", "test");//The result returned is the length of the array alert (Colors.join ("|"));
/The result is red|green|blue|white|test//add element var colors to the beginning of the array = ["Red", "green", "Blue", "test"]; var item = colors.unshift ("First");//The beginning of the array adds an element alert (Colors.join ("|"));
/return the last array//delete element var colors = ["Red", "green", "Blue", "test"]; var item = Colors.pop ()//Returns the deleted option result test alert (Colors.join ("|"));
/returns the final array result red|green|blue//delete the opening element var colors = ["Red", "green", "Blue", "test"]; var item = Colors.shift ();//Deletes the first option alert (Colors.join ("|") of the array); /returns the last array-------------------------------------------------//array sequence case//order reversal var colors = ["Red", "green", "Blue", "test"]
;Colors.reverse ();
alert (colors);//The result is: test,blue,green,red//array sort var values = [0,1,5,10,7];
Values.sort (Compare);
alert (values);

Document.writeln (values);
	} function Compare (value1,value2) {if (value1<value2) {return 1;
	}else if (value1>value2) {return-1;
}else return 0;
-----------------------------------------------------//Add array to array concat () method var colors = ["Color", "red"];
var colors2 = Colors.concat (["CCC", "BBBB"], ' 3333 ', [' vvccxx ', [' oolll ', ' lll ']]; alert (colors2);//Returns the result is: Color,red,ccc,bbbb,3333,vvccxx,oolll,lll//slice () method copying elements in an array does not break the previous element var colors = ["Color"
, "Red", ' eeee ', ' 221111 ']; var colors2 = Colors.slice (1);//Starting from 1 Replication alert (colors2);//The result is: red,eeee,221111 var colors = ["Color", "red", ' eeee ', '
221111 ']; var colors2 = Colors.slice (1,3);//Copy from 1 to 3rd position end alert (colors2);/The result is red,eeee-----------------------------------
----------------------------------//Array Delete element var a = [1,2,3,5,8]; var r = a.splice (0,2);
Delete the top 2 alert (a);//The result is 3,5,8 var a = [1,2,3,5,8]; VAr r = a.splice (1,1,100,200); Deletes an item starting at number 2nd and then inserts alert (a); the result is 1,100,200,3,5,8.

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.