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

array.remove(value);

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


当前回答

通过我的解决方案, 您可以在纯 JavaScript 的阵列中删除一个或多个项。 不需要另外的 JavaScript 库 。

var myArray = [1,2,3,4,5]; // First array

var removeItem = function(array,value) {  // My clear function
    if(Array.isArray(value)) {  // For multi remove
        for(var i = array.length - 1; i >= 0; i--) {
            for(var j = value.length - 1; j >= 0; j--) {
                if(array[i] === value[j]) {
                    array.splice(i, 1);
                };
            }
        }
    }
    else { // For single remove
        for(var i = array.length - 1; i >= 0; i--) {
            if(array[i] === value) {
                array.splice(i, 1);
            }
        }
    }
}

removeItem(myArray,[1,4]); // myArray will be [2,3,5]

其他回答

我也碰到了这样的情况:我不得不从阵列中移除一个元素。 indexof没有在互联网探索器中工作, 因此我分享我的工作 jQuery.inArray () 解决方案 :

var index = jQuery.inArray(val, arr);
if (index > -1) {
    arr.splice(index, 1);
    //console.log(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]

大部分答案都用...

循环的索引和组合删除过滤器

虽然所有解决方案都应该采用这些方法,但我想我们可以使用弦操纵。

需要注意的关于这一解决办法的要点 -- --

这将在数据中留下漏洞( 可以通过额外的过滤器将其移除 ) 此解决方案不仅适用于原始搜索值, 也适用于对象 。

诀窍是...

字符串字符串返回分隔符上的分解数据,以空字符串替换输入数据集中的搜索值。

    remove = (input, value) => {
        const stringVal = JSON.stringify(value);
        const result = JSON.stringify(input)

        return result.replace(stringVal, "").split(",");
    }

本文创建了一个测试对象和数字的 JSF 中心- https://jsfidle.net/4t7zhkce/33/

检查小提琴中的移除方法 。

我测试了复数和过滤器 看哪个更快:

let someArr = [... Array(999999). keys ()] 控制台.time(“ filter” ) someArr. filter(x x = = = = = 666666) 控制台.timeEnd(“ filter ” ) 控制台.time(“ splice by indexof ” ) someArr. splice( some Arr. indexof( 6666), 1) 控制台.timeEnd( “ splice by indexof ” )

在我的机器上, 复数会更快。 这有道理, 因为复数只编辑一个已有的数组, 而过滤器会创建一个新的数组 。

因此,过滤器在逻辑上是更清洁的(更容易阅读),并且更适合使用不可改变状态的编码风格。因此,由你来决定你是否想做出这种权衡。

有许多方法可以从 JavaScript 阵列中去除一个特定元素。 下面是我研究中可以找到的五种最佳方法。

1. 直接使用“ splice () ” 方法

在以下代码段,从数组中删除特定预定位置的元素。

语法: 数组_ name.spice( begin_ index, 数字_ elements_ remove) ; 应用程序 :

var arr = [1、2、3、4、5、6、7、8、9、10]; 控制台.log ("原件阵列: "+arr " ; var remote = ar.spice(4、2); 控制台.log ("修改后的阵列: "+arr " ; 控制台.log ("元素删除: "+删除 " );

2. 使用“ splice() () ” 方法用“ valice () ” 方法用“ value () ” 删除元素

在以下代码段中,我们可以使用循环内的条件,删除所有等于预先确定值(前:所有元素等于值6)的所有元素。

var arr = [1, 2, 6, 3, 2, 6, 7, 8, 8, 9, 10] 控制台.log ("原件阵列: "+arr”; 对于 (var i = 0; i < arr. 长度; i++) { { {var remove = rr.spice(i, 1); i-; }} 控制台. log ("修改阵列: "+arr " ); / / 6 被移除控制台. log ("重新删除的元素: "+删除 ");

3. 使用“过滤器()”方法删除按值选择的元素

与使用“ spice() () ” 方法的执行类似, 但它没有改变现有的数组, 而是创造了新的元素阵列, 从而删除了不想要的元素 。

var 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10] ; var 过滤 = 数组. filter( 函数( 值, 索引, rr) { 返回值 = 6 ; } ; 控制台. log ("原件数组 : "+array" ); 控制台. log ("新建数组 : "+filtered" ; / 6 被删除

4. 在“ Lodash” JavaScript 库中使用“ remove () ” 方法

在下面的代码段中, JavaScript 库里有叫作“ Lodash ” 的删除() 方法。 这个方法也与过滤方法相似 。

var 数组 = [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10] 控制台. log ("原件数组: "+数组: "+数组: "+ + + + 数组: "; var remote Eplement = _.remove (ary, 函数(n) { return n\\ 6; }) 控制台. log ("修改后的数组: "+ 数组: "+ 数组: "removed 元素: "+ removeElement 元素 " ); / / 6 被删除 < src=" https://cdn.jsdelivr.net/npm/lodash@4.17.21lodash.min.js" / stamp >

5. 制作自定义除去方法

JavaScript没有本地的“ array. remove” 方法, 但我们可以创建一种使用以上方法的方法, 用于在以下代码片段中执行 。

var 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10] ; 函数数组 remove( arve, value) { 返回 arr. filter( 函数( 元素) { 返回元素 $= = 值;} } 控制台. log (" 原件数组: "+ 数组 " ); 控制台. log (" 修改数组: "+ trightremove( 数组, 6) ); / / 6 被删除 。

最后方法(第5项)更适合解决上述问题。