1、创建数组
var test = ["Cain", "wish"];或者是 var test = new Array();
2、合并两个数组-contact()
var test1 = ["Cain", "w"];
var test2 = ["nihau", "what", "Dao"];
var test3 = ["luo"];
var children = test1.concat(test2,test3);
3、用数组的元素组成字符串-join()
function myFunction(){
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var x=document.getElementById("demo");
x.innerHTML=fruits.join();
}
4、数组转换成字符串-toString()
function myFunction(){
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var str = fruits.toString();
var x=document.getElementById("demo");
x.innerHTML= str;
}
5、删除数组的最后一个元素-pop(),第一个元素-shift()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction(){
fruits.pop();
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
6、给数组末尾添加新元素-push()
var fruits = ["Banana", "Orange", "Apple", "Mango"];
function myFunction(){
fruits.push("Kiwi")
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
7、数组的排序-sort()
function myFunction(){
//按字母顺序
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
//按数字升序
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return a-b});
//按数字降序
var points = [40,100,1,5,25,10];
points.sort(function(a,b){return b-a});
}
8、在数组的某个位置添加元素-splice()
function myFunction(){
var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.splice(2,0,"Lemon","Kiwi");
var x=document.getElementById("demo");
x.innerHTML=fruits;
}
9、反转数组顺序-reverse
10、返回数组对象的原始值-valueOf() [注:valueOf()不会改变原来的数组]
var fruits = ["Banana", "Orange", "Apple", "Mango"];
var v=fruits.valueOf();
v==fruits=>true