參考自: 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. 项目Beta冲刺(团队)--4/7

    课程名称:软件工程1916|W(福州大学) 作业要求:项目Beta冲刺 团队名称:葫芦娃队 作业目标:进行新一轮的项目冲刺,尽力完成并完善项目 团队博客 队员学号 队员昵称 博客地址 04160242 ...

  2. “为了交项目干杯”对“那周余嘉熊掌将得队”、“男上加男,强人所男”的Beta产品测试报告

    "为了交项目干杯"对"那周余嘉熊掌将得队"."男上加男,强人所男"的Beta产品测试报告 格式描述 课程名称:软件工程1916|W(福州大学 ...

  3. Python - 100天从新手到大师

    简单的说,Python是一个“优雅”.“明确”.“简单”的编程语言. 学习曲线低,非专业人士也能上手 开源系统,拥有强大的生态圈 解释型语言,完美的平台可移植性 支持面向对象和函数式编程 能够通过调用 ...

  4. python列表各种切片姿势

    顺着切,反着切,想怎么切就怎么切,但是别被坑. mylist = [1,2,3,4,5,6,7,8,9] print(mylist[2:7:2]) # [3, 5, 7] print(mylist[: ...

  5. 前段大数据传输 压缩解压 pako

    我已经找到了解决的办法:用pako.inflate(); 在页面引入链接即可调用pako

  6. reactnative 笔记

    1.<FlatList/> _renderItem = ({item})=>{ return <View style={[styles.part4Row]}> <T ...

  7. 分享一个简单易用的软件定时器模块(MultiTimer)——基于keil+stm32f103zet+hal库(裸机实现)

    公众号上看到一个比较好的一个github项目:https://github.com/0x1abin/MultiTimer 今天看了看,简单的,就移植了- 且看文档的说明, ============== ...

  8. brew 又叫Homebrew,是Mac OSX上的软件包管理工具

    brew 又叫Homebrew,是Mac OSX上的软件包管理工具; Homebrew是一款Mac OS平台下的软件包管理工具,拥有安装.卸载.更新.查看.搜索等很多实用的功能.简单的一条指令,就可以 ...

  9. Ferguson游戏&&Ua12293——打表找规律

    题意 有两个盒子分别有m颗糖果和n颗糖果,每次移动是将一个盒子清空而把另一个盒子里得一些糖果拿到被清空的盒子,使得两个盒子至少各有一个.无法移动者输. 分析 设初始状态为(m, n),显然(1, 1) ...

  10. VSCode 如何操作用户自定义代码片段

    自己写了一些根据自己习惯弄成的自定义代码片段,不喜跳过 很简单,快速过一下,F1,然后输入 snippets vue代码片段 { // Place your snippets for vue here ...