如何从数组中删除一个特定值? 类似 :

array.remove(value);

制约:我必须使用核心 JavaScript 。 框架不允许 。


当前回答

我本人也有这个问题(在更换阵列是可以接受的情况下),

var filteredItems = this.items.filter(function (i) {
    return i !== item;
});

要给上面的片段略加上下文:

self.thingWithItems = {
    items: [],
    removeItem: function (item) {
        var filteredItems = this.items.filter(function (i) {
            return i !== item;
        });

        this.items = filteredItems;
    }
};

此解决方案应该同时使用引用项和值项。 它都取决于您是否需要保持对原始数组的引用, 以判断该解决方案是否适用 。

其他回答

这取决于你是否想保持一个空位。

如果您想要一个空位 :

array[index] = undefined;

如果你不想空空的位子:

//To keep the original:
//oldArray = [...array];

//This modifies the array.
array.splice(index, 1);

如果您需要该项的值, 您可以保存返回的数组元素 :

var value = array.splice(index, 1)[0];

如果您想要在数组的两端删除, 您可以在最后一个或数组中使用数组. pop () 。 第一个或数组中可以使用数组. translate. pop () 。 translate () toft () for first one (两者都返回项目值 ) 。

如果您不知道项目的索引, 您可以使用数组。 indexof( 项目) 来获取它( 在 if( ) 中获得一个项, 或者在 a while( ) 中获取全部项 ) 。 数组。 indexof( 项目) 返回索引, 或 - 1 , 如果找不到的话 。

如果您想要 [.].remove(el) 类似语法的语法, 和其他编程语言一样, 那么您可以添加此代码 :

// Add remove method to Array prototype
Array.prototype.remove = function(value, count=this.length) {
    while(count > 0 && this.includes(value)) {
        this.splice(this.indexOf(value), 1);
        count--;
    }
    return this;
}

用法

// Original array
const arr = [1,2,2,3,2,5,6,7];

// Remove all 2s from array
arr.remove(2); // [1,3,5,6,7]

// Remove one 2 from beginning of array
arr.remove(2, 1); // [1,2,3,2,5,6,7]

// Remove two 2s from beginning of array
arr.remove(2, 2); // [1,3,2,5,6,7]

您可以根据您的需求操控该方法 。

您只需要按元素或索引过滤 :

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);

您可以使用复数来从数组中删除对象或值。

让我们考虑一个长度为5的行距, 值为10,20,30,40,50, 我想从中去掉30的值。

var 数组 = [10,20,30,40,50];如果 (数组. indexof(30) > -1) {数组.spice( 数组. indexof(30), 1);} 控制台. log( 数组); / / [10,20, 40,50]

业绩 业绩业绩 业绩业绩

今天(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