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

array.remove(value);

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


当前回答

最简单的方法可能是使用过滤功能。例如:

let array = ["hello", "world"]
let newarray = array.filter(item => item !== "hello");
console.log(newarray);
// ["world"]

其他回答

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

ES6且无突变:(2016年10月)

const removeByIndex = (list, index) =>
      [
        ...list.slice(0, index),
        ...list.slice(index + 1)
      ];
         
output = removeByIndex([33,22,11,44],1) //=> [33,11,44]
      
console.log(output)

Screenshot of demo

2021年更新

您的问题是如何从数组中删除一个特定项目。 您在具体项目中指的是一个数字, 例如 。 从数组中删除数字 5 。 据我了解, 您正在寻找类似 :

// PSEUDOCODE, SCROLL FOR COPY-PASTE CODE
[1,2,3,4,5,6,8,5].remove(5) // result: [1,2,3,4,6,8]

至于2021年,实现该目标的最佳途径是使用数组过滤功能:

const input = [1,2,3,4,5,6,8,5];
const removeNumber = 5;
const result = input.filter(
    item => item != removeNumber
);

以上例子的使用数组. prototype. filter函数。它会对所有数组项目进行迭代,并且只返回符合箭头函数结果,旧阵列保持不变,而一个新的阵列则被称为result包含不等于 5 的所有项目。您可以自己测试它在线在线.

你可以直观地看到数组. prototype. filter像这样 :

Animation visualizing array.prototype.filter

考虑考虑的考虑

守则质量

Array.prototype.filter在此情况下,这是消除数字的最容易读懂的方法,对错误几乎没有留有余地,并使用联署材料的核心功能。

为什么不array.prototype.map?

Array.prototype.map有时被视作一种替代array.prototype.filter对于此用途的话, 使用它。 但不应该使用它。 原因是数组. prototype. filter概念上用于过滤过滤项目能够满足箭头函数(精确我们需要的)的箭头功能,而数组. prototype.map用于变换项。由于在对项目进行循环时,我们不更改项目,使用的适当功能是array.prototype.filter.

支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助

截至今天(11.4.2022)94.08%的互联网用户'浏览器支持iE6 - 8 不支持它。 所以, 如果您的使用案例需要支持这些浏览器, 将会有一个不错的多元填充克里斯·费迪南蒂的作品

业绩 业绩业绩 业绩业绩

Array.prototype.filter对于大多数使用过的个案来说是巨大的。然而,如果您在寻找先进数据处理的性能改进,您可以探索一些高级数据处理的性能改进。其它选项像使用纯for。另一个伟大的选项是重新思考您正在处理的数组是否真的必须如此大。这可能是一个信号,即 JavaScript 应该从数据源获得一个减少的数组来进行处理。

不同可能性的基准:https://jsben.ch/C5MXz

从数组中删除项目的最佳方法是使用过滤法。.filter()返回没有过滤过的项目的新数组。

items = items.filter(e => e.id !== item.id);

这个.filter()将当前项目推到过滤的阵列中。更多信息过滤过滤器 在这里.