JavaScript data structures are continuously updated in: It'll be over in a week.

Source: Internet
Author: User

Array

Array creation:

var troop=New Array (6);                    // Create an array of length 6 var troop=New Array (2,3,5,6,4,7) ;

Array method:

varStr= "I love JavaScript";varSingle=str.split ("");//' I ', ', ' l ', ' O ',.....varMutipy=str.split ("");//' I ', ' love ', ' JavaScript 'vartroop=NewArray (2,5,6,8,9,4,1,2);varIndex=troop.indexof (2);//index=0varnames=[' Jack ', ' Mike ', ' Molly '];varStr=names.join ();//str= ' jack,mike,molly 'varnums=[];nums.push (3,4); Nums.shift ();//nums=[1,2,3,4];Nums.pop ();//nums=[1,2,3];Nums.shift ();//nums=[2,3];Nums.splice (0,1,5,6)//nums=[5,6,3];
Operations on array elements (no new arrays are generated)varnums=[2,4,6,8];functionIsEven (num) {return num%2==0;}
function Square (num) {return num*num;}
Nums.foreach (square); Squared all elements of an array
var swit=nums.every (IsEven); Determines whether the array element is even, if it returns true;

To generate a new array:
Filter,map

Two-tuple array:
for (Var i=0;i<5;i++)
Troop[i]=[];

Stacks and lists are actually packages of array and array methods, so I omit not to write.

Linked list:

    varlinklist=function(){        varNode=function(Element) { This. element=element;  This. next=NULL;        };  This. length=0;  This. head=NewNode ("Head");        Defines the chain header, length, and, at the same time, defines four methods.  This. append=append;  This. insert=Insert;  This. remove=remove;  This. display=display; functionAppend (Element) {varNewnode=NewNode (Element); The list adds a new node at the end, noting that Node.next is still the node type, so you call the nodes constructor when you add a new nodevarThisnode= This. Head;
this.length++; if(thisnode.element==NULL)//When writing loop statements or judging statements, be aware that an empty node has no element elements, {//So first make sure that the thi Snode is a non-empty node, the If judgment statement can run normally, or it will error. Thisnode.element=element; }Else{ while(thisnode.next) {Thisnode=Thisnode.next; } Thisnode.next=NewNode; } }; functionInsert (elementbe,element) {varNodebe= This. Head; this.length++; varInsertnode=NewNode (Element); while(! (nodebe.element===elementbe)) {Nodebe=Nodebe.next; }; varNodeafter=Nodebe.next; Nodebe.next=Insertnode; Insertnode.next=Nodeafter; }; functionRemove (Element) {varNodedelete= This. Head; while(! (nodedelete.next.element===element)) {Nodedelete=Nodedelete.next; }; varNodeafter=nodedelete.next.next;//next can be superimposed multiple timesnodedelete.next=Nodeafter; }; functiondisplay () {varThisnode= This. Head; varStr= ""; while(thisnode) {str+=thisnode.element+ ""; Thisnode=Thisnode.next; } returnstr; } }; varnewlist=Newlinklist (); If the call constructor is not quite understood with new, you can refer to the previous post for a brief analysis of this newlist.append ("1"); Newlist.append ("2"); Newlist.append ("3"); Newlist.append ("4"); Newlist.insert ("3", "5") varStr=Newlist.display (); alert (str);

JavaScript data structures are continuously updated in: It'll be over in a week.

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.