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 ...
随机推荐
- 修复wecenter移动版description首页描述一样问题
因网友要求,wecenter移动版description首页描述一样,所以在此写个教程,希望帮助大家! 修改方法 打开app/m/main.php TPL::output('m/question'); ...
- 服务器端渲染 VS 浏览器端渲染
这里的渲染,就是指生成html文档的过程,和浏览器渲染html没有关系. 浏览器端渲染,指的是用js去生成html,前端做路由.举例:React, Vue等等前端框架.适合单页面应用程序. 服务器端渲 ...
- CCF 201909-4 推荐系统
CCF 201909-4 推荐系统 试题编号: 201909-4 试题名称: 推荐系统 时间限制: 5.0s 内存限制: 512.0MB 问题描述: 算法设计 由于我们需要选出得分最大的K件商品,得出 ...
- 洛谷P2038 无线网络发射器选址
题目描述 随着智能手机的日益普及,人们对无线网的需求日益增大.某城市决定对城市内的公共场所覆盖无线网. 假设该城市的布局为由严格平行的 \(129\) 条东西向街道和 \(129\) 条南北向街道所形 ...
- Promise对异步编程的贡献以及基本API了解
异步: 核心: 现在运行的部分和将来运行的部分之间的关系 常用方案: 从现在到将来的等待,通常使用一个回调函数在结果返回时得到结果 控制台(因为console族是由宿主环境即游览器实现的)可能会使用异 ...
- 条件随机场CRF原理介绍 以及Keras实现
本文是对CRF基本原理的一个简明的介绍.当然,“简明”是相对而言中,要想真的弄清楚CRF,免不了要提及一些公式,如果只关心调用的读者,可以直接移到文末. 图示# 按照之前的思路,我们依旧来对比一下普通 ...
- module 'torch' has no attribute 'gesv'
新版torch不支持gesv,使用solve函数. Kt, _ = torch.gesv(P.mm(H.t()).t(), S) 改成 Kt, _ = torch.solve(P.mm(H.t()). ...
- 集合类 Map接口 HashTable
集合类的另外一种重要实现为Map接口,Map接口提供的方法如下: Map接口一个不常见实现为HashTable,HashTable对所有有并发访问问题的方法通过 synchronized 关键字进行并 ...
- Java的string类为什么是不可变的
最流行的Java面试题之一就是:什么是不可变对象(immutable object),不可变对象有什么好处,在什么情况下应该用,或者更具体一些,Java的String类为什么要设成immutable类 ...
- adb server version (31) doesn't match this client (41); killing...
1.有时候用adb工具去连接安卓设备,或者模拟器的时候,会提示adb server version(31) doesn’t match this client(41)这样的提示.如图 提示的字面意思就 ...