首页 > 其他分享 >js清空数组的正确做法

js清空数组的正确做法

时间:2022-11-24 17:38:30浏览次数:32  
标签:task 数组 myArray js length way 清空 做法 array


Emptying an array is a common JavaScript task but too often I see the task performed in the incorrect way. Many times developers will create a new array:

myArray = []; // bad

That isn’t the optimal way to get a fresh array; to truncate an array, and thus empty it, you should set the length property to zero:(正确的做法)

myArray.length = 0; // good!


标签:task,数组,myArray,js,length,way,清空,做法,array
From: https://blog.51cto.com/u_15890522/5884375

相关文章

  • angularjs解决播放语音出错的问题
    首先是看了github上的这个​​https://github.com/angular/angular.js/issues/1352​​,知道了语音播放不了了,下面是正确的播放代码首先添加过滤器app.filter("trustUrl",['......
  • angularjs解析html字符串
    参考链接​​https://docs.angularjs.org/api/ngSanitize​​首先下载angular-sanitize.min.js,下载地址:​​https://github.com/angular/bower-angular-sanitize​​添加解......
  • js判断是否是Json对象
    有两种方法1.使用正则表达式(推荐),代码如下:if(/^[\],:{}\s]*$/.test(text.replace(/\\["\\\/bfnrtu]/g,'@').replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[e......
  • Node.js使用path模块处理文件路径
    首先需要引入path模块varpath=require('path');1.文件路径处理:path.normalize(p)。path模块中的normalize()方法用来规范化路径字符串。可用于处理路径中的”//”、”........
  • php二维数组转一维数组的方法
    如下二维数组$arr=array(1){[0]=>array(1){["imgurl"]=>string(79)"/img/upload/microChatData/1/20161221/thumb/621fc7668474b47b43......
  • js对Json数组进行深复制
    在开发微信小程序的时候,很多时候我们传json对象的时候都需要小心,因为传进去的都是引用,所以有深复制Json对象的需求,方法如下:varbasicInfoTmp=JSON.parse(JSON.stringify(u......
  • jsTree使用
    jsTree可以显示一个树状视图,支持复选框选中,选中触发事件等:其中主要用到的方法有:1.设置数据:这里的data一般是ajax请求服务器返回的,必须要有id,parent,text这三个字段用于显......
  • 力扣349 两个数组的交集
    题目:给定两个数组 nums1 和 nums2,返回它们的交集 。输出结果中的每个元素一定是唯一的。我们可以不考虑输出结果的顺序。示例:输入:nums1=[1,2,2,1],nums......
  • js实现动态时间功能,实时时间自走动功能
    js实现动态时间功能,实时日期自走动功能效果如下:html页面js方式实现1.获取实时日期<divid="headerTime"class="header-timetext"><span......
  • Dayjs常用获取日期方法
    1.获取当天的日期dateFormat(dayjs().endOf('day'));2.获取当前周的起止日期constoneDayTime=24*60*60*1000;consttime=dayjs().endOf('week')......