代码描述:js数组 元素清空。js如何将数组中的元素清空
删除数组中所有的元素的方法有多种,下面就简单扼列举几种。 方法一:
<script> var theArray=["51前端","青岛市南区",2]; console.log(theArray.length); theArray.splice(0,theArray.length); console.log(theArray.length); </script>
使用splice()函数可以删除数组中所有的元素。 关于splice()函数可以参阅JavaScript splice()一章节。 方法二:
<script> var theArray=["51前端","青岛市南区",2]; console.log(theArray.length); theArray.length=0; console.log(theArray.length); </script>
将数组的length属性值设置为0即可将数组清空。 方法三:
<script> var theArray=["51前端","青岛市南区",2]; var beifen=theArray; console.log(theArray.length); theArray=[]; console.log(theArray.length); console.log(beifen.length); </script>
请绑定手机号,在继续操作
注意:只有绑定手机以后,才可使用网站全部功能