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

array.remove(value);

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


当前回答

根据索引删除

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

/**
* 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]

其他回答

虽然大多数先前的答复都回答了这个问题,但是为什么没有使用切片()方法还不够清楚。 是的,过滤器()符合不可改变的标准,但做以下更短的等值如何?

const myArray = [1,2,3,4];

现在让我们说我们应该从阵列中删除第二个元素, 我们可以简单地做到:

const newArray = myArray.slice(0, 1).concat(myArray.slice(2, 4));

// [1,3,4]

今天,社区强烈鼓励从阵列中删除元素,因为其简单和不可改变的性质。 一般来说, 导致突变的方法应该避免。 例如, 鼓励您将推( ) 替换为 concat () , 并用切片 () 替换为 plus () 。

我设定了一个函数:

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

并用它这样:

pop(valuetoremove, myarray);

使用 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/)

 (function removeFromArrayPolyfill() {
      if (window.Array.prototype.remove) return;
    
      Array.prototype.remove = function (value) {
        if (!this.length || !value) return;
    
        const indexOfValue = this.indexOf(value);
    
        if (indexOfValue >= 0) {
          this.splice(indexOfValue, 1);
        }
      };
    })();
    
    // testing polyfill
    const nums = [10, 20, 30];
    nums.remove(20);
    console.log(nums);//[10,30]
function array_remove(arr, index) {
    for (let i = index; i < arr.length - 1; i++) {
        arr[i] = arr[i + 1];
    }
    arr.length -= 1;
    return arr;
}
my_arr = ['A', 'B', 'C', 'D'];
console.log(array_remove(my_arr, 0));