数组的基本用法:
<script>
//定义一个arr数组
var arr = new Array();
//向数组的末尾添加一个a元素
arr.push('a');
//获取所有数组元素
//alert(arr); //a
//获取数组中的第一个元素【默认从0开始】
//alert(arr[0]); //a
//获取数组的长度
//alert(arr.length); //1
</script>
数组的shift方法实例:
<script>
//shift:删除原数组的第一项,并返回删除元素的【值】;如果数组为空则返回undefined
var a = [1,2,3,4,5];
var b = a.shift();
//alert(a); //a:[2,3,4,5]
//alert(b); //b:1
</script>
数组的unshift方法实例:
<script>
//unshift:将参数添加到数组的开头,并返回数组的【长度】
var a = [1,2,3,4,5];
var b = a.unshift(6,7);
//alert(a); //a:[6,7,1,2,3,4,5]
//alert(b); //b:7
</script>
数组的pop方法实例:
<script>
//pop:删除原数组中最后一项,并返回删除元素的【值】,如果数组为空则返回undefined
var a = [1,2,3,4,5];
var b = a.pop();
//alert(a); //a:[1,2,3,4]
//alert(b); //b:5
</script>
数组的push方法实例:
<script>
//push:将参数添加到原数组的末尾,并返回数组的【长度】
var a = [1,2,3,4,5];
var b = a.push(6,7);
//alert(a); //a:[1,2,3,4,5,6,7]
//alert(b); //b:7
</script>
数组的contact方法实例:
<script>
//contact:返回一个新数组,是将参数添加到原数组中构成的新数组
var a = [1,2,3,4,5];
var b = a.concat(6,7);
//alert(a); //a:[1,2,3,4,5]
//alert(b); //b:[1,2,3,4,5,6,7]
</script>
splice
<script>
//splice(start,deleteCount,val1,val2,...):从start位置开始删除deleteCount项,并从该位置起插入val1,val2,...
var a = [1,2,3,4,5];
var b = a.splice(2,2,6,7,8); //从数组a的第2项(对应元素:3)开始,删除2项(3,4),并从该位置起插入6,7,8
//alert(a); //a:[1,2,6,7,8,5]
//alert(b); //b:[3,4]
--------------------------------------------------------------------------------------------------------------------------
//splice的四种用法(同shift,unshift,pop,push)
var a = [1,2,3,4,5];
var b = a.splice(0,1); //同shift【删除数组第一项】
//alert(a); //a:[2,3,4,5]
//alert(b); //1
--------------------------------------------------------------------------------------------------------------------------
var a = [1,2,3,4,5];
a.splice(0,0,-2,-1); //同unshift【从数组的开头添加】
var b = a.length;
//alert(a); //a:[-2,-1,1,2,3,4,5]
//alert(b); //b:7
--------------------------------------------------------------------------------------------------------------------------
var a = [1,2,3,4,5];
var b = a.splice(a.length-1,1); //同pop【删除最后一项】
//alert(a); //a:[1,2,3,4]
//alert(b); //b:5
--------------------------------------------------------------------------------------------------------------------------
var a = [1,2,3,4,5];
a.splice(a.length,0,6,7); //同push【从数组末尾添加】
var b = a.length;
//alert(a); //a:[1,2,3,4,5,6,7]
//alert(b); //b:7
</script>
数组的reverse方法实例:
<script>
//reverse:将数组反序
var a = [1,2,3,4,5];
var b = a.reverse();
//alert(a); //a:[5,4,3,2,1]
//alert(b); //b:[5,4,3,2,1]
</script>
数组的sort方法实例:
<script>
//sort(orderfunction):按指定的参数对数组进行排序
var a = [1,2,3,4,5,11,22,33];
var b = a.sort();
//alert(a); //a:[1,11,2,22,3,33,4,5]
//alert(b); //b:[1,11,2,22,3,33,4,5]
--------------------------------------------------------------------------------------------------------------------------
//通过比较函数,将数组a按照从小到大进行排序
var a = [1,2,3,4,5,33,22,11];
//a.sort(new function(){'num1','num2','return num1-num2'}); //按照字符编码的顺序进行排序 1,11,2,22,3,33,4,5
a.sort(function compare(num1,num2){ return num1-num2;}); //从大到小排序:return num2-num1
for(var i =0; i<a.length; i++){
//document.write(a[i] + ' '); //1 2 3 4 5 11 22 33
}
--------------------------------------------------------------------------------------------------------------------------
var compare = function(num1,num2){ return num1-num2};
var a = [1,2,3,4,5,33,22,11];
a.sort(compare);
for(var i =0; i<a.length; i++){
document.write(a[i] + ' '); //1 2 3 4 5 11 22 33
}
</script>
数组的slice方法实例:
<script>
//slice(start,end):返回从原数组中指定开始下标到结束下标(不包含该元素)之间的项组成的新数组
var a = [1,2,3,4,5];
var b = a.slice(2,4);
//alert(a); //a:[1,2,3,4,5]
//alert(b); //b:[3,4]
</script>
<script>
数组的join方法实例:
<script>
//join(separator):将数组的元素组成一个字符串,以separator为分隔符,省略的话则用默认用逗号为分隔符
var a = [1,2,3,4,5];
var b = a.join('|');
//alert(a); //a:[1,2,3,4,5]
//alert(b); //b:1|2|3|4|5
</script>