如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
使用.indexof () 和.spolice () - 可变模式
这里有两种情况:
我们知道指数
康斯特饮料 = [“Tea”、“咖啡 ”、“Milk ”]; 康斯特 id = 1; 康斯特 删除饮料 = 饮料.splice(id, 1); 控制台.log (removedDrink)
我们不知道指数,但知道它的价值。 Const drinks = [“Tea”,“Coffee”,“Milk”]; const id = drinks.indexof (“Coffee”);// 1 Const moved Drink = drink.spice(id,1);// / [“Coffee”] 控制台.log(removedDrink);// [“Tea”,“Milk” 控制台.log(drinks);
使用. filter () - 不可变模式
你可以想到的最好方式是 — — 而不是“删除”该项目,而是“创造”一个新的阵列,只是不包含该项目。 所以我们必须找到它,完全省略它。
康斯特饮料 = [“Tea”、“Coffee”、“Milk ”]; Const id = “Coffee”; Const idx = drinks.indexof(id); 康斯特 移除的Drink = 饮料 [idx]; 康斯特过滤的饮料 = 饮料 ; Const 过滤的Drinks = 饮料 . filter ((饮料,索引) 饮料 = 去除的Drink); 控制室.log (“ Filter 饮料阵列 :”+过滤的饮料 ); 控制室.log (“ 原件饮料阵列 :” +饮料 );
其他回答
从数组中删除一个特定元素, 可以在过滤器选项的一行内完成, 它得到所有浏览器的支持 : 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
我不知道你如何期待阵列.remove( int) 的行为。 我可以想到三种可能性, 你可能会想要。
要删除索引 i 中的数组元素:
array.splice(i, 1);
如果您想要从数组中删除带有数值数的每个元素 :
for (var i = array.length - 1; i >= 0; i--) {
if (array[i] === number) {
array.splice(i, 1);
}
}
如果您只想在索引 I 中使元素不再存在, 但是您不想让其它元素的索引改变 :
delete array[i];
Array.prototype.remove = function(x) {
var y=this.slice(x+1);
var z=[];
for(i=0;i<=x-1;i++) {
z[z.length] = this[i];
}
for(i=0;i<y.length;i++){
z[z.length]=y[i];
}
return z;
}
使用 indexof 查找要删除的数组元素的索引,然后用复数删除该索引。
组合法通过删除现有元素和/或添加新元素来改变数组的内容。
const 数组 = [2, 5, 9] ; 控制台. log( 数组); const 指数 = 数组. indexof (5); 如果 (index > - 1) {/ / 仅当发现项目时 复数组 。 spice (index, 1) ; / 2nd 参数意味着只删除一个项 } // 数组 = [2, 9] 控制台. log( 数组) ;
复数的第二个参数是要删除的元素数量。 请注意, 复数会修改所在的数组, 并返回包含已删除元素的新数组 。
由于完整性的原因,此处为函数。第一个函数只删除一个单一事件(即从 [2,5,9,1,5,5,8,5] 中删除第一个匹配5),而第二个函数删除所有事件:
{ var indexof( val值); 如果 (index > - 1 ) { rr. spice( index, 1); } 返回 arr; } 函数删除TroitAll( arr, val值 = 0); { (i) < arr. long) { (arr) {( i) { rr. spice( i, 1); { other { +gi; } } 返回 arr; } / 使用控制台. log( removeteTunce ([ 2, 5, 9, 1, 5, 5, 5, 5 5 ) 控制台. log( removete TempallAll ( 2, 5, 9, 1, 5, 5, 5, 5) ) )
在类型Script中,这些函数可用类型参数保持类型安全:
function removeItem<T>(arr: Array<T>, value: T): Array<T> {
const index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
我刚刚通过对象.defineProperty在 Array. prototype 上创建了一个多填充器, 以便删除一个阵列中想要的元素, 而不会导致在... 的... 。
if (!Array.prototype.remove) {
// Object.definedProperty is used here to avoid problems when iterating with "for .. in .." in Arrays
// https://stackoverflow.com/questions/948358/adding-custom-functions-into-array-prototype
Object.defineProperty(Array.prototype, 'remove', {
value: function () {
if (this == null) {
throw new TypeError('Array.prototype.remove called on null or undefined')
}
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'object') {
if (Object.keys(arguments[i]).length > 1) {
throw new Error('This method does not support more than one key:value pair per object on the arguments')
}
var keyToCompare = Object.keys(arguments[i])[0]
for (var j = 0; j < this.length; j++) {
if (this[j][keyToCompare] === arguments[i][keyToCompare]) {
this.splice(j, 1)
break
}
}
} else {
var index = this.indexOf(arguments[i])
if (index !== -1) {
this.splice(index, 1)
}
}
}
return this
}
})
} else {
var errorMessage = 'DANGER ALERT! Array.prototype.remove has already been defined on this browser. '
errorMessage += 'This may lead to unwanted results when remove() is executed.'
console.log(errorMessage)
}
删除整数值
var a = [1, 2, 3]
a.remove(2)
a // Output => [1, 3]
删除字符串值
var a = ['a', 'ab', 'abc']
a.remove('abc')
a // Output => ['a', 'ab']
删除布尔值
var a = [true, false, true]
a.remove(false)
a // Output => [true, true]
也可以通过此 Array. prototype. remove 方法从数组中移除对象。您只需要指定要删除对象的键值 {% 。
删除对象值
var a = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 3, b: 2}]
a.remove({a: 1})
a // Output => [{a: 2, b: 2}, {a: 3, b: 2}]