參考自: https://love2dev.com/blog/javascript-remove-from-array/

1. Removing Elements from End of Array

var ar = [1, 2, 3, 4, 5, 6];
ar.length = 4; // set length to remove elements
console.log( ar ); // [1, 2, 3, 4] var ar = [1, 2, 3, 4, 5, 6];
ar.pop(); // returns 6
console.log( ar ); // [1, 2, 3, 4, 5]

2. Removing Elements from Beginning of Array

var ar = ['zero', 'one', 'two', 'three'];
ar.shift(); // returns "zero"
console.log( ar ); // ["one", "two", "three"]

3. Using Splice to Remove Array Elements

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var removed = arr.splice(2,2); /*
removed === [3, 4]
arr === [1, 2, 5, 6, 7, 8, 9, 0]
*/
["bar", "baz", "foo", "qux"]

list.splice(0, 2)
// Starting at index position 0, remove two elements ["bar", "baz"] and retains ["foo", "qux"].

4. Removing Array Items By Value Using Splice

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

for( var i = 0; i < arr.length; i++){
if ( arr[i] === 5) {
arr.splice(i, 1);
}
} //=> [1, 2, 3, 4, 6, 7, 8, 9, 0]

5. Using the Array filter Method to Remove Items By Value

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];

var filtered = array.filter(function(value, index, arr){

    return value > 5;

});

//filtered => [6, 7, 8, 9]
//array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

6. The Lodash Array Remove Method

var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) {
return n % 2 === 0;
}); console.log(array);
// => [1, 3] console.log(evens);
// => [2, 4]

7. Making a Remove Method

function arrayRemove(arr, value) {

   return arr.filter(function(ele){
return ele != value;
}); } var result = arrayRemove(array, 6); // result = [1, 2, 3, 4, 5, 7, 8, 9, 0]

8. Explicitly Remove Array Elements Using the Delete Operator

var ar = [1, 2, 3, 4, 5, 6];
delete ar[4]; // delete element with index 4
console.log( ar ); // [1, 2, 3, 4, undefined, 6]
alert( ar ); // 1,2,3,4,,6

9. Clear or Reset a JavaScript Array

var ar = [1, 2, 3, 4, 5, 6];
//do stuff
ar = [];
//a new, empty array! var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1; // Reference arr1 by another variable
arr1 = [];
console.log(arr2); // Output [1, 2, 3, 4, 5, 6] var ar = [1, 2, 3, 4, 5, 6];
console.log(ar); // Output [1, 2, 3, 4, 5, 6]
ar.length = 0;
console.log(ar); // Output [] var ar = [1, 2, 3, 4, 5, 6];
console.log(ar); // Output [1, 2, 3, 4, 5, 6]
ar.splice(0, ar.length);
console.log(ar); // Output [] var ar = [1, 2, 3, 4, 5, 6];
console.log(ar); // Output [1, 2, 3, 4, 5, 6]
while (ar.length) {
ar.pop();
}
console.log(ar); // Output []

最新文章

  1. Objective-C 语法之 static 关键字
  2. Vi 几个实用的命令
  3. spoj 95
  4. zookeeper[2] zookeeper原理(转)
  5. Intellij Idea 12 加载weblogic8X的插件
  6. HTML5之Canvas影片广场
  7. SparkMLlib学习之线性回归
  8. SpringMVC框架(三)from标签(转)
  9. Linux 手册惯用的节名
  10. RBS SharePoint 2010 Server.wmv
  11. Linux LVM使用小记
  12. hustoj 管理员和后台设置
  13. 丑女贝蒂第一至四季/全集Ugly Betty迅雷下载
  14. Cas Server源码编译现场实例
  15. 实验一 ASP.NET应用环境配置 总结
  16. windowSoftInputMode
  17. Linux 删除用户时报错:userdel: user zhoulijiang is currently used by process 1
  18. flask第十八篇——模板【2】
  19. “全栈2019”Java第七十六章:静态、非静态内部类访问权限
  20. vue-cli 基础搭建

热门文章

  1. 通过jar包名称,获取maven的依赖信息GAV
  2. Ice Igloos Gym - 101480I (暴力技巧)
  3. Linux下Python3源码安装
  4. mvc基础配置
  5. robotframework 文档
  6. 【Beta】“北航社团帮”测试报告——小程序v2.0与网页端v1.0
  7. WINDOWS 命令行调用SAS代码 并指定输出路径 示例
  8. mybatis自定义之优先从classes目录加载,加载之后遇到相同的类定义时不再加载
  9. 012 webpack中的router
  10. FreeSWITCH视频直播