length
设置或返回数组中元素的数目
var arr=[0,1,2,3,4,5]
document.write(arr.length); //6
prototype
向对象添加属性
function employee(name, job, born) {
this.name = name;
this.job = job;
this.born = born;
}
var bill = new employee("Bill Gates", "Engineer", 1985);
employee.prototype.num=20000;
document.write(bill.num);//20000
concat()
连接两个或多个的数组,并返回结果。
var arr0=[0,0,0,0,0];
var arr1=[1,2,3,4,5];
var arr2=[6,7,8,9,0];
var arr3=arr1.concat(arr2,arr0);
document.write("concat:" + arr3 + "<br>");//concat:1,2,3,4,5,6,7,8,9,0,0,0,0,0,0
join()
可以将数组转化为指定格式的字符串
var arr4=["努","力","活","成","最","美","的","样","子"];
document.write(arr4 + "<br>"); //努,力,活,成,最,美,的,样,子
document.write(arr4.join() + "<br>"); //努,力,活,成,最,美,的,样,子
document.write(arr4.join("") + "<br>"); //努力活成最美的样子
document.write(arr4.join("*") + "<br>"); //努*力*活*成*最*美*的*样*子
push()和unshift()
push() :向数组尾部添加一个或多个元素并返回现有的长度
unshift():向数组头部添加一个或多个元素并返回现有的长度
var arr2=[6,7,8,9,0];
document.write(arr2.push("一","二","三","四","五") + "<br>"); //数组长度 10
document.write(arr2 + "<br>"); //6,7,8,9,0,一,二,三,四,五
// unshift()向数组头部添加一个或多个元素并返回现有的长度
document.write(arr2.unshift("一","二","三","四","五") + "<br>"); //数组长度 15
document.write(arr2 + "<br>"); //一,二,三,四,五,6,7,8,9,0,一,二,三,四,五
pop()和shift()
pop() 删除并返回数组的最后一个元素
shift() 删除并返回数组的第一个元素
var arr1=[1,2,3,4,5];
document.write(arr1.pop() + "<br>");
document.write(arr1 + "<br>");
// shift() 删除并返回数组的第一个元素
document.write(arr1.shift() + "<br>");
document.write(arr1 + "<br>");
reverse()
颠倒数组中元素的顺序
var arr1=[1,2,3,4,5];
document.write(arr1.reverse() + "<br>");
slice()
从某个已有的数组返回选定的元素 arrayObject.slice(start,end)
var arr=[1,2,3,4,5];
document.write(arr.slice(2,4) + "<br>"); //3 4
splice()
删除元素,并向数组添加新元素
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
arr.splice(2,0,"William") //位置 删除个数 添加元素
document.write(arr + "<br />")
sort()
用于对数组的元素进行排序
var arr = [1, 100, 5, 20];
console.log(arr.sort()); // [1, 100, 20, 5]
console.log(arr); // [1, 100, 20, 5] (原数组改变)
升序
var arr = [10, 100, 500, 20];
function sortNumber(a, b) {
return a - b
};
console.log(arr.sort(sortNumber)); //[10, 20, 100, 500]
console.log(arr); //[10,100,500,20] (原数组改变)
降序
var arr = [10, 100, 500, 20];
function sortNumber(a, b) {
return b - a
};
console.log(arr.sort(sortNumber)); // [500, 100, 20, 10]
console.log(arr); // [10, 100, 500, 20] (原数组改变)