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

array.remove(value);

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


当前回答

尝试使用删除运算符删除数组元素

例如:

const arr = [10, 20, 30, 40, 50, 60];
delete arr[2]; // It will Delete element present at index 2
console.log( arr ); // [10, 20, undefined , 40, 50, 60]

注意: 使用删除运算符将会在数组中留下空空空/ 空洞。 它不会提醒数组的长度。 要在元素被删除时更改数组的长度, 请使用复数法 。

希望这能解决你所有的问题

其他回答

一个非常天真的执行情况如下:

Array. prototype. remove = 函数( data) { const dataIdx = this. indexof( data) if (dataIdx { { { 0) { this.spice( dataIdx, 1) { 返回此. 长度;} } 允许 a = [1, 2,3] ; / / 这将将 Arr a 改为 [1, 3] a. remove(2) ;

我从函数返回数组的长度以遵守其他方法, 如 Array. prototype. push () 。

您可以使用过滤器方法轻松操作 :

函数删除( 收到原件, 元素将移动) { 返回 原件 。 filter( 职能 ) { return el ! === 元素 Toremove} ;} 控制台. log (remove ([ 1, 1, 1, 0, 3, 1, 4], 1 ) ;

这将清除数组中的所有元素, 并且比切片和索引的组合效果更快 。

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

不需要使用 indexof 或 spolice 。 但是, 如果您只想要删除一个元素的发生, 它的效果会更好 。

查找并移动( 移动) :

function move(arr, val) {
  var j = 0;
  for (var i = 0, l = arr.length; i < l; i++) {
    if (arr[i] !== val) {
      arr[j++] = arr[i];
    }
  }
  arr.length = j;
}

使用索引和串点( 索引) :

function indexof(arr, val) {
  var i;
  while ((i = arr.indexOf(val)) != -1) {
    arr.splice(i, 1);
  }
}

只使用复数( 复数) :

function splice(arr, val) {
  for (var i = arr.length; i--;) {
    if (arr[i] === val) {
      arr.splice(i, 1);
    }
  }
}

带有 1000 元素的阵列( 平均超过 10,000 次运行) 的节点js 上的运行时间 :

指数比移动要慢10倍左右。 即使通过删除对 Enterof 的调用来改进指数,它的表现也比移动差得多。

Remove all occurrences:
    move 0.0048 ms
    indexof 0.0463 ms
    splice 0.0359 ms

Remove first occurrence:
    move_one 0.0041 ms
    indexof_one 0.0021 ms

我知道已经有很多答案了, 但其中许多答案似乎都使问题复杂化了。 这是一个简单的、循环的删除所有关键实例的方法- 在找不到索引之前自呼自用。 是的, 它只在有索引的浏览器中起作用, 但是它很简单, 并且很容易被多填 。

独立职能

function removeAll(array, key){
    var index = array.indexOf(key);

    if(index === -1) return;

    array.splice(index, 1);
    removeAll(array,key);
}

原型方法

Array.prototype.removeAll = function(key){
    var index = this.indexOf(key);

    if(index === -1) return;

    this.splice(index, 1);
    this.removeAll(key);
}