Use of array in JavaScript

Source: Internet
Author: User

The array objects in JavaScript are arrays, first a dynamic array, and an ultra-strong complex like C #, "Arrays, lists, hashmap/dictionary" in Java.

Array Arrays

How to use:
Example 1:

var citys = new Array ();//Create an array object without initializing the length, dynamic
Citys[0] = ' Shanghai ';
Citys[1] = ' Beijing ';
CITYS[2] = ' Shenzhen ';

for (var i=0; i< citys.length; i++) {
Alert (Citys[i]);
}

Example 2 ( recommended ):

var arr=[10,true, ' Wow ka ka '];
for (Var i=0;i<arr.length;i++) {
Alert (Arr[i]);
}

Forin traversing array arrays

var arr=[' Hani ', ' Zhuang ', ' Manchu ', ' Drift tribe '];
For (var item in arr) {
Alert (item+ ' = = = ' +arr[item]);
}

array Key-value pairs

How to use

For example:

var arr=new Array ();
arr["name"]= ' Jack ';
arr["Age"]=28;
arr["Gender"]= ' fale ';
arr["Height"]=180;
For (var item in arr) {
Alert (item+ ' = = = ' +arr[item]);
}

Traversing an array key-value pair

Forin Cycle

var arr={name: ' James ', age:28};
For (var item in arr) {
Alert (item+ ' = = = ' +arr[item]);
}

Through the object. Key

var arr={name: ' James ', age:28};
alert (arr.name);
alert (arr.age);

Method Pass Value

function F1 (s) {
alert (s.name);
alert (s.age);
}
F1 ({name: ' Rose ', age:20});

Special guy. Example 1:

var arr=[{name: ' James ', age:19},{name: ' Rose ', age:18},{name: ' Max ', age:20}];
for (Var i=0;i<arr.length;i++) {
Alert (arr[i].name+ ' = = = ' +arr[i].age);
}

Example 2:

var code={name: ' Max ', Children:[{name: ' Jack ', Age:20},{name: ' Rose ', Age:18}]};
alert (code.name);
for (Var i=0;i<code.children.length;i++) {
Alert (code.children[i].name+ ' >> ' +code.children[i].age);
}

Of course Forin can also, but trouble

For (var item in Code.children) {
Alert (item+ ' >> ' +code.children[item].name+ ' = = = ' +code.children[item].age);
}

Array Comparison

Customizing a common comparison method


        function Max (arr,compare) {
            var m=arr[0];
            for (var i=1;i<arr.length;i++) {
                if (compare (arr[i],m) ) {
                     M=arr[i];
               }
           }
            return m;
       }


var S1=max ([12,32,33,100,8],function (N1,N2) {
Return n1>n2;
});
alert (S1);

var s2=max (["AA", "BBBB", "CCC"],function (N1,N2) {
Return n1.length>n2.length;
});

alert (S2);

Comparison method in JavaScript sort ()

Example 1:

var arr=[23,12,56,200,45];
Arr.sort (function (n1,n2) {
Return n1>n2;
});
Alert (arr);

var arrstr=["AA", "BBB", "CCCC"];
Arrstr.sort (function (n1,n2) {
Return n1.length>n2.length;
});
alert (ARRSTR);

Example 2:

var arr=[{name: ' Bob ', age:100},{name: ' Tom ', age:23},{name: ' Rose ', age:78}];
Arr.sort (function (n1,n2) {
Return n1.age>n2.age;
});
for (Var i=0;i<arr.length;i++) {
alert (arr[i].name);//tom,rose,bob
}

Use of array in JavaScript

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.