如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
这里有很多奇妙的答案, 但对我来说,最有效的答案 不是完全从阵列中移除我的元素, 而是简单地设定它的价值为无效。
这对大多数情况都有效,而且是一个很好的解决方案,因为我稍后会使用变量,而不想让变量消失,只是暂时空的。此外,这个方法完全可以交叉浏览。
array.key = null;
其他回答
如果您在数组中有复杂的对象, 您可以使用过滤器 。 在 $. in Array 或 tray. splice 不容易使用的情况下 。 特别是如果对象在数组中可能是浅的 。
例如,如果您有一个带有 Id 字段的对象,而您想要从数组中删除该对象:
this.array = this.array.filter(function(element, i) {
return element.id !== idToRemove;
});
答案已经很多了, 但是因为还没有人用一个衬里来做, 我想我会展示我的方法。 它会利用字符串. split () 函数在创建数组时将删除所有指定字符这一事实。 这里举一个例子 :
var ary = [1、2、3、4、1234、10、4、5、7、3]; out = arry.join (" -" -").split ("-4 -").join (" -").split (" -").split (" -");control.log(out) ;
在此示例中, 所有 4 个的字符都在从数组中移除 。 但是, 必须指出, 包含字符“ - ” 的任何数组都会与此示例产生问题 。 简而言之, 这会导致组合( “ - ” ) 函数不适当地将您的字符串拼凑在一起。 在这种情况下, 上面的扇形中的所有“ - ” 字符串都可以替换为在原始数组中不会使用的任何字符串 。 以下还有一个示例 :
var ary = [1,2,3,4,'-',1234,10,'-',4,5,7,3]; out = ary.join("!@#").split("!@#4!@#").join("!@#").split("!@#"); console.log(out);
使用 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;
}
除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...
const removeItem =
idx =>
arr =>
arr.reduce((acc, a, i) => idx === i ? acc : acc.concat(a), [])
const array = [1, 2, 3]
const index = 1
const newArray = removeItem(index)(array)
console.log(newArray) // logs the following array to the console : [1, 3]
...或者一个循环函数(诚实地说不是那么优雅...也许有人有更好的循环解决方案? ? )...
const removeItemPrep =
acc =>
i =>
idx =>
arr =>
// If the index equals i, just feed in the unchanged accumulator(acc) else...
i === idx ? removeItemPrep(acc)(i + 1)(idx)(arr) :
// If the array length + 1 of the accumulator is smaller than the array length of the original array concatenate the array element at index i else...
acc.length + 1 < arr.length ? removeItemPrep(acc.concat(arr[i]))(i + 1)(idx)(arr) :
// return the accumulator
acc
const removeItem = removeItemPrep([])(0)
const array = [1, 2, 3]
const index = 1
const newArray = removeItem(index)(array)
console.log(newArray) // logs the following array to the console : [1, 3]
对我而言,越简单越好,2018年(2019年左右),我给你这个(近一点)单行话,回答最初的问题:
Array.prototype.remove = function (value) {
return this.filter(f => f != value)
}
有用的是,你可以用在咖喱的表达方式上,比如:
[1,2,3].remove(2).sort()