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

array.remove(value);

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


当前回答

如果元素存在多个实例,您可以进行后回循环,以确保不破坏索引。

var myElement = "chocolate"; var my Array = [“cocolate ”、“potart”、“potart”、“potart”、“potart”、“cocolate”、“potart”、“potart”、“cococolate ”]; / * 重要代码 * / 用于 (var i = my Array. lary - 1; i & 0; i-) {如果 (my Array [i] = my Element) my Array.spolice (i, 1);}控制台.log (my Array); {如果 (my Array [i] = my Element) my Array.splice (i, 1);} 控制台(my Array);

其他回答

您可以使用 JavaScript 的标准 __ proto__ 来定义此函数。例如,

让数据 = [;] 数据 .__proto__.remove = (n) { {数据 = 数据 : blockMap ((v) ) { { 返回 v ? v : [] n? v ; 数据 = [1, 2, 3] ; 数据 = [1, 2, 3] ; 数据.remove (2); 控制台.log(数据); / / [1,3] 数据 = [a', b', c'] ; 数据.remove ('b'); 控制台.log(数据); // [a, c]

Array. prototype. removeByValue = 函数( val) {, 对于 (var i = 0; i < this. long; i++) { 如果 (i) val) { this. splice (i, 1); i- ; } 返回此 ;} var falts = [apple, 'banana', 'carrot', 'orange' ] ; falves.remove ByValue ('banana'); 礼服. log (fruts); / / - > [apple, 'carrot', 'orange' ]

我对基底 JavaScript 阵列进行了相当高效的扩展:

Array.prototype.drop = function(k) {
  var valueIndex = this.indexOf(k);
  while(valueIndex > -1) {
    this.removeAt(valueIndex);
    valueIndex = this.indexOf(k);
  }
};

使用 JavaScript 从数组中删除一个项的几种方法 。

所描述的所有方法都不改变原始数组,而是创建一个新的数组。

如果您知道某个项目的索引

假设您有一个阵列, 您想要删除位置 i 上的项目 。

一种方法是使用切片( ) :

const items = [a], 'b','c','d','e','f'] const i = 3 const 过滤项目 = items.selice(0, i).concat( items.selice( i+1, items. long)) 控制台.log( 过滤项目)

切片 () 创建新数组, 使用它收到的索引 。 我们只需创建一个新数组, 从开始到要删除的索引, 并且将另一个数组从我们删除后的第一个位置集中到数组的末尾 。

如果您知道数值

在这种情况下,一个很好的选择是使用过滤器(),这提供了一种更宣示性的方法:

Const 项 = [“ a ”、“ b ” 、 “ c ” 、 “ d ” 、 “ e ” 、 “ f ” 、 “ f ” 等值 Toremove = “ c” 等式过滤项目 = 项. filter (项目 项 ! ! ! = ex 值 to remove) 控制台.log (过滤项目 )

此选项使用 ES6 箭头函数。 您可以使用传统函数支持旧的浏览器 :

const items = ['a', 'b','c','d','d','e','f'] const value to remove ='c' const 过滤项目= items. filter(功能(项目) {返回项目!}== value to remove}控制台.log(过滤项目)

或者你可以使用 Babel 将ES6代码转换回ES5, 使旧浏览器更容易消化, 而在您的代码中写入现代 JavaScript 。

删除多个项目

如果不是单项,而是要删除许多项,会怎么样?

让我们找到最简单的解决方案

按指数分列的指数

您可以在序列中创建函数并删除项目 :

const items = [a], b', c', d', d', e', e', e', f'] const demote =( items, i) = items = (items, i, i' i) {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{}}}}}} {{{{{{{{{{{{{{{{{{{{}}}}}} {{{{{{{{{{{{{{}}}{{{{{{{{{{{{{{}}}}{{{{{{{{{{{{{{{{{{{}}}} {{{{{{{{{{{{{}}}}}{{{{{{{{}}}}}}}{{{{{{{{}}}}}}{{{{{{{{{{{{}}}}}}}}}}}{{{{{{{{{}}}}}}}}}}}}}}}{{{{{}}}}}}}{{}}}}{{{{{}}}}}{{{{{}}}}}}}}}{{{{{{{{{{{{{{{{}}}}}{{{{{{{{{{}}}}}}}}{{{{}}}}{{{{{{{{{{}}}}{}}}}}}}}}{}}}}}}}}}}}}}}}}}}{}}}}}}}}}}}{{}{{}{}{}}}}{{}}}}}}}}}}{}{}{}

以数值计

您可以在回溯函数中搜索包含内容 :

const items = ['a', 'b', 'c','d','d','e','f'] const value to remove = ['c','d'] comst filtems = items. filter(项目 \\\ ? $values to remove.) 包括(项目) // ["a","b","e","f" 控制台。log(过滤项目)

避免突变原始数组

plice() spolice () (不得与切片() 相混淆) 使原始数组变形, 并应避免 。

(最初张贴在我的网站上:https://flaviocopes.com/how-to-remove-itm-from-array/)

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