參考自: 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 []

Javacript Remove Elements from Array的更多相关文章

  1. leetcode 283 Move Zeros; 27 Remove Elements; 26 Remove Duplicated from Sorted Array;

    ,,,,}; //把数组的值赋给vector vector<int> vec(arr, arr+sizeof(arr)/sizeof(int)); 解法一: 时间复杂度O(n) 空间复杂度 ...

  2. Leetcode: Remove Elements

    Given an array and a value, remove all instances of that value in place and return the new length. T ...

  3. 【Remove Elements】cpp

    题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...

  4. Remove duplicates from array II

    //Given a sorted array, remove the duplicates in place such that each element appear only // once an ...

  5. Remove duplicates from array

    //Given a sorted array, remove the duplicates in place such that each element appear only // once an ...

  6. Interview Return Products of All Other Elements in Array

    这是一道面试亚马逊时的题目,要求Time O(n). 我刚开始想的是算出所有的数的总product,再去除以对应位置的元素,但这种做法的问题是若该位置为0,就会报错. 到网上搜了下,才知道,原来有这种 ...

  7. Foreach & add remove elements

    参考 http://stackoverflow.com/questions/11058384/how-to-delete-specific-array-elements-from-within-a-f ...

  8. max (Largest elements in array)

    句法: M = max(A) M = max(A,[],dim) [M,I] = max(___) C = max(A,B) ___ = max(___,nanflag)   描述: M=max(A) ...

  9. How do I remove a particular element from an array in JavaScript?

    9090down voteaccepted Find the index of the array element you want to remove, then remove that index ...

随机推荐

  1. 一些坑 Java 执行命令行命令 Spring Boot 打包为jar ResourceUtils.getFile 等出现的问题

    Java 执行命令行命令 这个没技术含量的东西耗费了我半个多小时 String command = ....; Process process = Runtime.getRuntime().exec( ...

  2. 【PHP】系统部署

    1.MySql数据库,单独创建用户和数据库 使用MySql-Front导入,避免使用Navicat导入 2.httpd-vhosts.conf配置 文件位于:D:\Xampp\apache\conf\ ...

  3. jquery-ui.min.js的draggable()拖拽功能

    <!doctype html> <html lang="en"> <head> <meta charset="utf-8&quo ...

  4. MSAA简介

    https://www.cnblogs.com/gnagwang/archive/2010/04/20/1716006.html MSAA的全称是Microsoft Active Accessibil ...

  5. 判断json对象是否在数组中

    // 判断对象是否在数组中function objinArrar(check,param){ var isExisted = false; var index = -1; for(var i=0;i& ...

  6. re.sub 实现多处替换

    1   | 表示或的意思 将所有字母替换掉 result_content = re.sub('a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z', ...

  7. MySQL8.0忘记密码后重置密码(亲测有效)

    实测,在mysql8系统下,用mysqld --console --skip-grant-tables --shared-memory可以无密码启动服务 服务启动后,以空密码登入系统 mysql.ex ...

  8. kafk设计要点

    kafka的设计目标是高吞吐量,所以kafka自己设计了一套高性能但是不通用的协议,他是仿照AMQP( Advanced Message Queuing Protocol   高级消息队列协议)设计的 ...

  9. NOI2019 回家路线 DP

    「NOI2019」回家路线 链接 loj 思路 f[i][j]第i个点,时间为j,暴力转移 复杂度O(m*t),好像正解是斜率优化,出题人太不小心了233 代码 #include <bits/s ...

  10. HHHOJ #151. 「NOI模拟 #2」Nagisa

    计算几何板子题(我才没有拷板子的说--) 众所周知,三角形的重心坐标是\((\frac{x_1+x_2+x_3}{3},\frac{y_1+y_2+y_3}{3})\) 然后我们发现如果我们有一个点集 ...