如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
更新: 只有当您无法使用 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]
其他回答
我对基底 JavaScript 阵列进行了相当高效的扩展:
Array.prototype.drop = function(k) {
var valueIndex = this.indexOf(k);
while(valueIndex > -1) {
this.removeAt(valueIndex);
valueIndex = this.indexOf(k);
}
};
业绩 业绩业绩 业绩业绩
今天(2019-12-09),我为选定的解决方案对macOS v10.13.6(高塞拉利昂)进行性能测试。我表示删除(A),但与其他方法相比,我没有使用,因为它在阵列中留下了空位。
结 结 结 结 结 结 结
最快的溶液是阵列。 splice (C) (对于大阵列、 阵列、 slice+splice (H) 是 Firefox 和 Safari 最快的不可改变的溶液 。 来自 (B) 的 Array. forest in Chrome mable 溶液通常比位于Safari 的小型表格的不可移动的快1.5x-6x, 令人惊讶的是, 变式溶(C) 慢于不可改变的溶液 (G) 。
详细细节
在测试中,我以不同的方式将中间元素从数组中去除。 A, C 的解决方案就位。 B, D, E, F, G, H 的解决方案是不可改变的 。
包含 10 元素的阵列结果
调
在铬中, 数组. spice (C) 是本地最快的解决方案。 数组. filter (D) 是最快的不可变的解决方案。 最慢的就是数组. sice (F)。 您可以在这里对您的机器进行测试 。
带有 1 000 000 元素的阵列结果
调
在铬中, 数组. spice (C) 是本地最快的解决方案( 删除( C) 的速率相似) - 但是在数组中留下了一个空位( 所以它不会执行“ 完全移除 ” ) 。 数组. sice- splice (H) 是最快的不可改变的解决方案。 最慢的就是数组. filter (D和E) 。 您可以在这里对您的机器进行测试 。
[var a = [0, 1, 2, 3, 4, 5, 6, 7, 9];var log = (letter, array) {(letter, trara.) log.log (leg. join `, `); 函数 A(arry) {var = trares[index]; {var (arr) {var = Array.(ar) 5; var. spice (al) = (aly) log (b) ; {var.log (ar) = listal. g(ary) = listal(5); trace. sloa. (var) log (var) = a. (l) a. (l) ; (var) lix (l) a. (l) a. (l) a. (l) ; (l) a. (l) a. (l) a. (l) a. (l) ex. (l) ex. (l) a. (l) a. (l) a. (l) a. (l) a. (l) a.
浏览器比较:Chrome v78.0.0,Safari v.13.0.4和Firefox v71.0.0
调
除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...
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]
您可以扩展数组对象以定义自定义的删除函数如下:
let let = [1, 2,4,4,4,5,5,45,9;] 数.delete = 函数(价值) { var indexof Target = this.indexof (value) =.indexof(value) = this.if(indexoftaget = = = = = = = = = = y= = y,4,4,4,5,4,459; 数. 数.delet = 函数(val = val indexof Talget = this. indexeof(valent) = = = = = indexget =.indexof = = =. { = indexexuf(val =) { = = indexete = = / / / imme eleteletelete (1) // / / / / 预期输出= / / / tradestrayf delett 删除 2,4,4,4,4,4,4,5,5,5,4,4,5,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,9/ /
您只需要按元素或索引过滤 :
var num = [5, 6, 5, 4, 5, 1, 5]; var result1 = num.filter((el, index) => el != 5) // for remove all 5 var result2 = num.filter((el, index) => index != 5) // for remove item with index == 5 console.log(result1); console.log(result2);