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

array.remove(value);

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


当前回答

Vanilla JavaScript(ES5.1) - 已建立版本

浏览器支持: Internet Explorer 9 或后( 详细浏览器支持)

/**
 * Removes all occurences of the item from the array.
 *
 * Modifies the array “in place”, i.e. the array passed as an argument
 * is modified as opposed to creating a new array. Also returns the modified
 * array for your convenience.
 */
function removeInPlace(array, item) {
    var foundIndex, fromIndex;

    // Look for the item (the item can have multiple indices)
    fromIndex = array.length - 1;
    foundIndex = array.lastIndexOf(item, fromIndex);

    while (foundIndex !== -1) {
        // Remove the item (in place)
        array.splice(foundIndex, 1);

        // Bookkeeping
        fromIndex = foundIndex - 1;
        foundIndex = array.lastIndexOf(item, fromIndex);
    }

    // Return the modified array
    return array;
}

Vanilla JavaScript(ES5.1) - 永恒版本

浏览器支持: 与原版的香草 JavaScript 相同

/**
 * Removes all occurences of the item from the array.
 *
 * Returns a new array with all the items of the original array except
 * the specified item.
 */
function remove(array, item) {
    var arrayCopy;

    arrayCopy = array.slice();

    return removeInPlace(arrayCopy, item);
}

Vanilla ES6 - 永恒版本

浏览器支持: Chrome 46, 边缘 12, Firefox 16, Opera 37, Safari 8 (详细浏览器支持)

/**
 * Removes all occurences of the item from the array.
 *
 * Returns a new array with all the items of the original array except
 * the specified item.
 */
function remove(array, item) {
    // Copy the array
    array = [...array];

    // Look for the item (the item can have multiple indices)
    let fromIndex = array.length - 1;
    let foundIndex = array.lastIndexOf(item, fromIndex);

    while (foundIndex !== -1) {
        // Remove the item by generating a new array without it
        array = [
            ...array.slice(0, foundIndex),
            ...array.slice(foundIndex + 1),
        ];

        // Bookkeeping
        fromIndex = foundIndex - 1;
        foundIndex = array.lastIndexOf(item, fromIndex)
    }

    // Return the new array
    return array;
}

其他回答

大部分答案都用...

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

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

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

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

诀窍是...

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

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

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

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

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

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

var index = jQuery.inArray(val, arr);
if (index > -1) {
    arr.splice(index, 1);
    //console.log(arr);
}

我认为许多 JavaScript 指令对于功能性编程没有很好的想法。 Splice 返回被删除的元素, 大部分时间您需要减少的数组。 这是不好的 。

想象一下您正在做一个循环调用, 并且不得不通过一个阵列, 并用一个更少的项目, 可能没有当前索引化的项目 。 或者想象一下您正在做另一个循环调用, 并且不得不通过一个带有元素推动的阵列 。

在这两种情况中,你都不能做我的递归功能(myArr.push(c))或我的递归功能(myArr.spice(i,1) ) 。 第一个白痴实际上会通过阵列的长度,第二个白痴会通过删除的元素作为参数。

所以事实上我所做的是... :删除一个阵列元素, 并将结果传递到一个参数的函数中, 同时我做如下:

myRecursiveFunction(myArr.slice(0,i).concat(a.slice(i+1)))

说到推,那更傻...

myRecursiveFunction((myArr.push(c),myArr))

我相信一种正确的功能语言 一种方法突变它所呼吁的物体 就必须返回一个引用 作为结果的物体本身。

根据索引删除

函数返回在索引中没有元素的数组副本 :

/**
* removeByIndex
* @param {Array} array
* @param {Number} index
*/
function removeByIndex(array, index){
      return array.filter(function(elem, _index){
          return index != _index;
    });
}
l = [1,3,4,5,6,7];
console.log(removeByIndex(l, 1));

$> [ 1, 4, 5, 6, 7 ]

以值删除

函数返回没有值的数组副本。

/**
* removeByValue
* @param {Array} array
* @param {Number} value
*/
function removeByValue(array, value){
      return array.filter(function(elem, _index){
          return value != elem;
    });
}
l = [1,3,4,5,6,7];
console.log(removeByValue(l, 5));

$> [ 1, 3, 4, 6, 7]

从数组中删除一个特定元素/字符串可在单班条中进行:

theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);

此处:

阵列:要从

将字符串从矩阵中删除:您想要删除的字符串, 1 是您想要删除的元素数量 。

注意 : 如果“ 字符串要从阵列中移除” 不位于数组中, 这将删除数组中的最后元素 。

在移除该元素之前先检查该元素是否存在于您的阵列中, 总是很好的做法 。

if (theArray.indexOf("stringToRemoveFromArray") >= 0){
   theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);
}

取决于客户电脑上是否有新版或旧版的剪贴条:

var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');

var array = ['1','2','3','4','5','6'];
var newArray = array.filter(function(item){ return item !== '3' });

“ 3” 是您想要从数组中删除的值。 数组会变成 : [“ 1 ” , “ 2 ” , “ 4 ” , “ 5 ” , “ 6 ”