Javacript Remove Elements from Array
參考自: 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的更多相关文章
- 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) 空间复杂度 ...
- Leetcode: Remove Elements
Given an array and a value, remove all instances of that value in place and return the new length. T ...
- 【Remove Elements】cpp
题目: Given an array and a value, remove all instances of that value in place and return the new lengt ...
- Remove duplicates from array II
//Given a sorted array, remove the duplicates in place such that each element appear only // once an ...
- Remove duplicates from array
//Given a sorted array, remove the duplicates in place such that each element appear only // once an ...
- Interview Return Products of All Other Elements in Array
这是一道面试亚马逊时的题目,要求Time O(n). 我刚开始想的是算出所有的数的总product,再去除以对应位置的元素,但这种做法的问题是若该位置为0,就会报错. 到网上搜了下,才知道,原来有这种 ...
- Foreach & add remove elements
参考 http://stackoverflow.com/questions/11058384/how-to-delete-specific-array-elements-from-within-a-f ...
- max (Largest elements in array)
句法: M = max(A) M = max(A,[],dim) [M,I] = max(___) C = max(A,B) ___ = max(___,nanflag) 描述: M=max(A) ...
- 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 ...
随机推荐
- com.alibaba.fastjson.JSONException: exepct '[', but error, pos 1, json : %255B%257B%2522list%2522%253A%255B%257B%2522itemId%2522%253A1369331%252C%2522num%2522%253A2%257D%255D%257D%255
com.alibaba.fastjson.JSONException: exepct '[', but error, pos 1, json : %255B%257B%2522list%2522%25 ...
- centos安全加固
设置SSH登录超时时间 /etc/profile export TMOUT=900 设置账户密码策略 /etc/login.defs PASS_MAX_DAYS 180 PASS_MIN_DAYS 0 ...
- JQuery系列(7) - JQuery最佳实践
上篇文章是一篇入门教程,从设计思想的角度,讲解"怎么使用jQuery".今天的文章则是更进一步,讲解"如何用好jQuery". 我主要参考了Addy Osman ...
- 监听localStorage中的数据变化
问题描述:我们在js里面获取了某一个localstorage的值,但是后期它可能改变了,我们js只执行一遍没办法再次获取它的值,当然可以刷新页面获取,但如果是我们的但页面就不能刷新页面了,此时:我们可 ...
- C程序运行原理
计算机不能直接识别和执行高级语言写的命令,必须用编译程序(也称编译器)把C源程序翻译成二进制形式的目标程序,然后再将该目标程序与系统的函数库以及其他目标程序连接起来,形成可执行的目标程序 C语言的编译 ...
- gym102222 G. Factories
gym102222 G. Factories 地址 题目大意: 给一棵n个点的树,选m个点,这m个点只能在叶子节点上,问着m个点中两两之间到达其余各点的距离和最小值是多少题解:任意两点的树上距离和问题 ...
- 【JZOJ6214】【20190614】tetris
题目 这是一道和俄罗斯方块有关的有趣题目 底面宽度为\(N\),高度无限,初始时方块高度为\(A_i\) 你可以决定每次会下落一个\(1 \times K\)或者\(K \times 1\)的方块 你 ...
- Vue与REACT两个框架的区别和优势对比
VUE和REACT两个JavaScript框架都是当下比较受欢迎的,他们两者之间的区别有那些,各自的优缺点是什么,本文将为你呈现. 简单介绍 除非你最近一直不关注前端的发展,不然你肯定听说过由Face ...
- docker之修改存储位置
#停止docker 1.systemctl stop docker 2.mkdir /home/docker-lib #在我这个项目里home是普通硬盘,在home下创建一个目录3.mv /var ...
- [Beta阶段]第七次Scrum Meeting
Scrum Meeting博客目录 [Beta阶段]第七次Scrum Meeting 基本信息 名称 时间 地点 时长 第七次Scrum Meeting 19/05/13 大运村寝室6楼 35min ...