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

array.remove(value);

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


当前回答

我设定了一个函数:

function pop(valuetoremove, myarray) {
    var indexofmyvalue = myarray.indexOf(valuetoremove);
    myarray.splice(indexofmyvalue, 1);
}

并用它这样:

pop(valuetoremove, myarray);

其他回答

对我而言,越简单越好,2018年(2019年左右),我给你这个(近一点)单行话,回答最初的问题:

Array.prototype.remove = function (value) {
    return this.filter(f => f != value)
}

有用的是,你可以用在咖喱的表达方式上,比如:

[1,2,3].remove(2).sort()

业绩 业绩业绩 业绩业绩

今天(2019-12-09),我为选择的解决方案对macOS v10.13.6(高塞拉利昂)进行性能测试。delete(A),但与其他方法相比,我并不使用它,因为它在数组中留下了空白。

结 结 结 结 结 结 结

  • 最快的解决方案是array.splice(C) (小型阵列有第二次的Safeare除外)
  • 以大数组盟誓,array.slice+splice(H) 是Firefox和Safari最快、不可改变的解决办法;Array.from(B) 铬含量最快
  • 可变溶液通常比不可变的更快1.5x-6x
  • 令人惊讶的是,变异解决办法(C)慢于不可变解决办法(G)

详细细节

在测试中,我以不同的方式从数组中删除中间元素。A、C本地的解决方案。B、D、E、F、G、H解决方案是不可改变的。

包含 10 元素的阵列结果

Enter image description here

在铬array.splice(C) 是当地最快的解决办法。array.filter(D) 是最快、不可改变的解决方案。array.slice(F). 您可以在机器上进行测试在这里.

带有 1 000 000 元素的阵列结果

Enter image description here

在铬array.splice(C) 是最迅速的就地解决办法delete(C) 的快速性相似,但它在数组中留下了一个空位(因此它不执行“完全移除”)。array.slice-splice(H) 是最快的不可改变的解决方案。 最慢的是array.filter(D和E). 您可以在您的机器上进行测试。在这里.

var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var log = (letter,array) => console.log(letter, array.join `,`);

function A(array) {
  var index = array.indexOf(5);
  delete array[index];
  log('A', array);
}

function B(array) {
  var index = array.indexOf(5);
  var arr = Array.from(array);
  arr.splice(index, 1)
  log('B', arr);
}

function C(array) {
  var index = array.indexOf(5);
  array.splice(index, 1);
  log('C', array);
}

function D(array) {
  var arr = array.filter(item => item !== 5)
  log('D', arr);
}

function E(array) {
  var index = array.indexOf(5);
  var arr = array.filter((item, i) => i !== index)
  log('E', arr);
}

function F(array) {
  var index = array.indexOf(5);
  var arr = array.slice(0, index).concat(array.slice(index + 1))
  log('F', arr);
}

function G(array) {
  var index = array.indexOf(5);
  var arr = [...array.slice(0, index), ...array.slice(index + 1)]
  log('G', arr);
}

function H(array) {
  var index = array.indexOf(5);
  var arr = array.slice(0);
  arr.splice(index, 1);
  log('H', arr);
}

A([...a]);
B([...a]);
C([...a]);
D([...a]);
E([...a]);
F([...a]);
G([...a]);
H([...a]);
This snippet only presents code used in performance tests - it does not perform tests itself.

浏览器比较:Chrome v78.0.0,Safari v.13.0.4和Firefox v71.0.0

Enter image description here

大部分答案都用...

  1. 索引和交点
  2. 删除删除
  3. 过滤过滤器
  4. 常设经常经常 经常经常for loop

虽然所有解决办法都应采用这些方法,但我认为我们可以使用字符串操作.

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

  1. 它会在数据中留下漏洞(可以通过额外的过滤器来清除)
  2. 此解决方案不仅用于原始搜索值,而且用于工作对象.

诀窍是...

  1. stringify数据集和搜索值
  2. 用空字符串替换输入数据集中的搜索值
  3. 返回返回split分隔符上的数据,.
    remove = (input, value) => {
        const stringVal = JSON.stringify(value);
        const result = JSON.stringify(input)

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

这里创建了一个测试对象和数字 JSF 的 JSF 。 。 。https://jsfiddle.net/4t7zhkce/33/

检查remove方法在小提琴中。

Vanilla JavaScript(ES5.1) - (ES5.1) -已经到位版本版本

浏览器支持 :因特网探索者 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) - (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);
}

香草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;
}
Array.prototype.removeItem = function(a) {
    for (i = 0; i < this.length; i++) {
        if (this[i] == a) {
            for (i2 = i; i2 < this.length - 1; i2++) {
                this[i2] = this[i2 + 1];
            }
            this.length = this.length - 1
            return;
        }
    }
}

var recentMovies = ['Iron Man', 'Batman', 'Superman', 'Spiderman'];
recentMovies.removeItem('Superman');