JavaScript数组函数

数组函数:

<script type="text/javascript">

var arr = ["George","John","Thomas","James","Adrew","Martin"]
var arr2 = ["arr2Value",2,55,"znf"]

document.write("-------------1、concat:连接多个的数组。不会改变现有的数组,而仅仅会返回被连接数组的一个副本------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.concat(1,2,arr2)+ "<br />")
document.write(arr + "<br />")
document.write("------------------concat--------end--------------------"+ "<br />")

document.write("-------------2、join(separator):将数组元素用指定的分隔符拼接成字符串后返回,不改变元素组------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.join("###")+ "<br />")
document.write(arr + "<br />")
document.write("------------------join--------end--------------------"+ "<br />")

document.write("-------------3、pop:出栈(删除并返回数组的最后一个元素)------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.pop()+ "<br />")
document.write(arr + "<br />")
document.write("------------------pop--------end--------------------"+ "<br />")

document.write("-------------4、push:入栈(数组末端增加一个或多个元素,返回新数组长度)------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.push("znf1","znf2")+ "<br />")
document.write(arr + "<br />")
document.write("------------------push--------end--------------------"+ "<br />")

document.write("-------------4、shift:出队(删除并返回数组第一个元素)------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.shift()+ "<br />")
document.write(arr + "<br />")
document.write("------------------shift--------end--------------------"+ "<br />")

document.write("-------------5、unshift:入队(在数组前端增加一个元素,并返回新数组长度)------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.unshift("znf1","znf2")+ "<br />")
document.write(arr + "<br />")
document.write("------------------shift--------end--------------------"+ "<br />")

document.write("-------------6、reverse:颠倒数组,改变数组------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.reverse()+ "<br />")
document.write(arr + "<br />")
document.write("------------------reverse--------end--------------------"+ "<br />")

document.write("-------------7、sort:数组排序,改变数组------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.sort() + "<br />")

function sortNumber(a, b)
{
return a - b
}
document.write(arr2.sort(sortNumber)+ "<br />")
document.write("------------------sort--------end--------------------"+ "<br />")

document.write("-------------8、splice(删除位置,删除数量,插入的元素1,...)------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.splice(2,2,"znf") + "<br />")
document.write(arr + "<br />")
document.write("------------------splice--------end--------------------"+ "<br />")

document.write("-------------9、slice(index,end) 返回包含下标为index到end-1元素的新数组------------"+ "<br />")
document.write(arr + "<br />")
document.write(arr.slice(2,5) + "<br />")
document.write(arr + "<br />")
document.write("------------------slice--------end--------------------"+ "<br />")
原文地址:https://www.cnblogs.com/weimengjiacan/p/13597673.html