如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
ES1010
该员额总结了从ECMAScript 2019(ES10)时的阵列中去除元素的通用方法。
1. 普通案件
1.1. 使用 .spice () 以值清除矩阵元素
| In-place: Yes | | Removes duplicates: Yes(loop), No(indexOf) | | By value / index: By index |
如果您知道要从数组中删除的值, 您可以使用复数法。 首先, 您必须确定目标项的索引。 然后使用索引作为起始元素, 并删除一个元素 。
// With a 'for' loop
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( let i = 0; i < arr.length; i++){
if ( arr[i] === 5) {
arr.splice(i, 1);
}
} // => [1, 2, 3, 4, 6, 7, 8, 9, 0]
// With the .indexOf() method
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const i = arr.indexOf(5);
arr.splice(i, 1); // => [1, 2, 3, 4, 6, 7, 8, 9, 0]
1.2. 使用. filter () 方法删除矩阵元素
| In-place: No | | Removes duplicates: Yes | | By value / index: By value |
特定元素可以通过提供过滤功能从数组中过滤出来。然后对数组中的每一元素要求此函数。
const value = 3
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => item !== value)
console.log(arr)
// [ 1, 2, 4, 5 ]
1.3. 通过扩展阵列.prototype来删除阵列元素
现职:是/否(取决于执行情况) 删除重复:是/否(取决于执行情况) 按数值/指数:按指数 / 按数值(取决于执行情况)
阵列原型可以通过其他方法扩展,然后在创建的阵列上使用这些方法。
注:从JavaScript标准图书馆(如阵列)扩展物体原型,被一些人视为反型。
// In-place, removes all, by value implementation
Array.prototype.remove = function(item) {
for (let i = 0; i < this.length; i++) {
if (this[i] === item) {
this.splice(i, 1);
}
}
}
const arr1 = [1,2,3,1];
arr1.remove(1) // arr1 equals [2,3]
// Non-stationary, removes first, by value implementation
Array.prototype.remove = function(item) {
const arr = this.slice();
for (let i = 0; i < this.length; i++) {
if (arr[i] === item) {
arr.splice(i, 1);
return arr;
}
}
return arr;
}
let arr2 = [1,2,3,1];
arr2 = arr2.remove(1) // arr2 equals [2,3,1]
1.4. 使用删除运算符删除队列元素
此处:是 删除重复: 否
使用删除运算符不会影响长度属性。 它也不会影响后续元素的索引。 数组变得稀少, 这是一种巧妙的方式, 表示删除的项目没有被删除, 但却没有定义 。
const arr = [1, 2, 3, 4, 5, 6];
delete arr[4]; // Delete element with index 4
console.log( arr ); // [1, 2, 3, 4, undefined, 6]
删除运算符的设计是为了从 JavaScript 对象中删除属性,而数组则是对象。
1.5. 使用物体功用(ES10)删除阵列元素
| In-place: No | | Removes duplicates: Yes | | By value / index: By value |
从 Entries 引入的 ES10 对象。 可用于在工艺过程中从任何类似 矩阵的物体中创建所需的矩阵和过滤不想要的元素。
const object = [1,2,3,4];
const valueToRemove = 3;
const arr = Object.values(Object.fromEntries(
Object.entries(object)
.filter(([ key, val ]) => val !== valueToRemove)
));
console.log(arr); // [1,2,4]
2. 特殊情况
2.1 如果在阵列末端,则删除元素
2.1.1. 改变阵列长度
此处:是 {__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
JavaScript 阵列元素可以通过将长度属性设定为低于当前值的值,从数组的末尾删除。任何指数大于或等于新长度的元素将被删除 。
const arr = [1, 2, 3, 4, 5, 6];
arr.length = 5; // Set length to remove element
console.log( arr ); // [1, 2, 3, 4, 5]
2.1.2. 使用.pop()方法
此处:是 {__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
流行方法删除数组最后一个元素, 返回该元素, 并更新长度属性。 流行方法修改援引该元素的数组, 这意味着与使用删除最后一个元素的方式不同, 将完全删除最后一个元素, 并缩小数组长度 。
const arr = [1, 2, 3, 4, 5, 6];
arr.pop(); // returns 6
console.log( arr ); // [1, 2, 3, 4, 5]
2.2. 如果在阵列开始时,则删除元素
此处:是 {__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
. toft () 方法与流行方法非常相似, 但它删除了 JavaScript 阵列的第一个元素, 而不是最后一个元素。 当元素被删除时, 其余元素会被移到下方 。
const arr = [1, 2, 3, 4];
arr.shift(); // returns 1
console.log( arr ); // [2, 3, 4]
2.3. 删除元素,如果它是阵列中唯一的元素
此处:是_________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
最快的技术是设置空数组的数组变量。
let arr = [1];
arr = []; //empty array
2.1.1的替代技术可通过将长度设定为0来使用。
其他回答
从数组中删除一个特定元素, 可以在过滤器选项的一行内完成, 它得到所有浏览器的支持 : https:// caniuse. com/ #search=filter% 20arary
function removeValueFromArray(array, value) {
return array.filter(e => e != value)
}
我在这里测试了此函数 : https://bit.dev/joshk/jotils/remove-value- from-array/~code#test.ts
您可以使用ES6. 例如,在此情况下删除值“ 3” :
var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');
console.log(newArray);
产出:
["1", "2", "4", "5", "6"]
咖啡:
my_array.splice(idx, 1) for ele, idx in my_array when ele is this_value
主要有两种办法:
当您对数组使用删除时要小心。 它有利于删除对象的属性, 但对于数组则不好。 最好对数组使用复数符。
请注意, 当您对数组使用删除时, 您可能会对 anArray. 长度得出错误的结果。 换句话说, 删除将会删除元素, 但不会更新长度属性的值 。
使用删除后,也可以期望在索引编号上出现空洞,例如,最后可能会出现第1、3、4、8、9和11号指数,其长度和之前一样被删除。在这种情况下,所有环形索引都会崩溃,因为指数不再相继。
如果您由于某种原因被迫使用删除, 那么当您需要通过数组循环时, 您应该对每个循环使用。 事实上, 如果可能的话, 总是避免使用已索引过的循环。 这样代码就会更加稳健, 并且不易遇到索引问题 。
更新: 只有当您无法使用 ECMAScript 2015 (前称ES6) 时, 才会推荐使用此方法。 如果您可以使用此方法, 其它答案可以提供更清晰的落实 。
此条格将解决您的问题, 并删除所有参数, 而不是仅一个( 或指定值 ) 。
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed
destroyed = this.splice(i, 1)[0];
}
}
return destroyed;
}
用法 :
var x = [1, 2, 3, 3, true, false, undefined, false];
x.destroy(3); // => 3
x.destroy(false); // => false
x; // => [1, 2, true, undefined]
x.destroy(true); // => true
x.destroy(undefined); // => undefined
x; // => [1, 2]
x.destroy(3); // => null
x; // => [1, 2]