javascript基础(数组操作2)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>jsarray</title>
	<script type="text/javascript">
	     //////数组截取/////////////
	   var arr=new Array(1,23,34,434,34);
           console.log(arr.length);
	   arr.length=3;
	   console.log(arr.length);
	   ////////元素分隔符//////////////
	   var array=['usa','uk','canada','brazil',5656];
	   var str1=array.join(',');
	   var str2=array.join('/');
	   console.log(str1);
	   console.log(str2);
	   ///////数组拼接/////////////////
	   var array2=[545,454,'gfgfgf','3344ff'];
	   console.log(array.concat(array2));
	   /////取出数组部分slice方法的第一个参数为起始位置,第二个参数为终止位置,操作/////不影响数组本身//
	   console.log(array.slice(1,4));///数组下边为1,截取(4-1)
	   ///////splice///////
	   array.splice(3,1);///数组下标为3的,删除一位
	   console.log(array);
        /////从数组下标为3开始,删除0位后,插入后面的值
       array.splice(3,0,'mmmm','cdcd','dd',89898,'end');
	   console.log(array);
	   ////数组排序////////
	   array.sort();
	   console.log(array);
	   numberarray=[34,344,434,3434,33,43,44,4,0,-454,45];
	   function sortnum(a,b){
	     return a-b; ///正序 b-a 为逆序
	   }
	   numberarray.sort(sortnum);
	   console.log(numberarray);
	   ////删除数组元素///////
	   Array.prototype.remove = function(from, to) {
			    var rest = this.slice((to || from) + 1 || this.length);
				this.length = from < 0 ? this.length + from : from;
				return this.push.apply(this, rest);
				};
	   numberarray.remove(3);
	   console.log(numberarray);
    </script>
</head>
<body>
	
</body>
</html>

你可能感兴趣的:(javascript基础(数组操作2))