【数组基础的增,删,改,查】

数组的添加和删除
<body>
		<script type="text/javascript">
			//操作数组  添加数据  删除 复制  查找
			var array = [1,2,3];
			// array[array.length]=4;
			//再数组的尾部添加 追加
			var res = array.push(5);
			console.log(res);//追加后返回数组的长度
			
			// 再数组的头部添加
			res = array.unshift(0);
			console.log(res);//返回数组的长度
			console.log(array);
			
			//删除数组第一位数据 
			res = array.shift();
			console.log(res);//返回被删除的第一位值
			console.log(array);
			
			//删除数组的最后一位
			res = array.pop();
			console.log(res);//返回被删除的最后一位值
			console.log(array);
			
			// 删除指定位数的元素 1  2  3
			console.log('--------splice-------------');
			// splice(1,2,10)从1号位置开始 删除2格元素 并将10从1号位置添加
			res = array.splice(1,2,10);//返回的结果是 被删除的元素
			console.log(res);
			console.log(array);
			// 1.只写一个参数 会从指定开始的位置开始删除直到数组的末尾  以数组的形式返回删除的元素
			//  2.写两个参数  会从指定的位置 删除指定个数的元素 并以数组的形式返回
			//  3.写三个参数  从指定位置开始 删除指定的元素,并将新元素从指定开始的位置添加   返回删除的元素
			// var arr = [1,2,3,4,5,6];
			// res = arr.splice(1);
			// console.log(res);
			// console.log(arr);
			
			// 查找数组中的元素 indexOf查找数组第一次出现的位置 如果有返回下标
			// 如果没有返回-1
			var array = [1,2,3];
			res = array.indexOf(5);
			console.log(res);// -1
			res = array.indexOf(2);
			console.log(res);//1
			var array = [1,2,3,4];
			
			console.log('--------slice-------------');
			//数组的截取 不影响原数组
			res = array.slice(2,3);//2是截取起始位置 3是结束位置 但是3号位置不包括再内
			 console.log(res);
			console.log(array);
			
			res = array.slice(1);//从1号位置开始截取到数组的尾部
			console.log(res);
			console.log(array);
			console.log('--------数组的拷贝-------------');
			//数组的拷贝
			res = array.slice();
			 console.log(res);
			console.log(array);
			
			console.log('--------concat-------------');
			//数组的连接
			var arr2=[5,6,7,8];
			res = array.concat(arr2);
			 console.log(res);
			
			res = array.concat();//实现数组的拷贝
			 console.log(res);
		</script>
		
		
	</body>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值