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

array.remove(value);

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


当前回答

我喜欢这个单行道:

arr.includes(val) && arr.splice(arr.indexOf(val), 1)

ES6 (无互联网探索者支持) 清除已就位 。 快速 : 没有重复重复或重复 。 支持删除无定义或未定义的值

作为原型

// remove by value. return true if value found and removed, false otherwise
Array.prototype.remove = function(val)
{
    return this.includes(val) && !!this.splice(this.indexOf(val), 1);
}

(是的,我读了所有其他的答案 找不到一个结合的答案 包含和串联在同一行。 )

其他回答

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 函数。 它会对所有数组项目进行循环, 并只返回符合箭头函数的项目。 因此, 旧数组保持不变, 而称为新数组的结果包含不等于5的所有项目。 您可以自己在网上测试它 。

您可以将数组. prototype. filter like this:

考虑考虑的考虑

守则质量

ARray. prototype. filter 是最容易读取的方法来删除这个例子中的数字, 它给错误留下很少的位置, 并使用联署材料的核心功能 。

为什么不设置数组. prototype.map?

ray. prototype.map 有时被视为用于此用途的数组. prototype. filter 的替代选项。 但不应使用它。 原因是数组. prototype. filter 概念上用来过滤符合箭头函数( 我们需要的精度)的项目, 而数组. prototype.map 则用来转换项目。 由于在对项目进行迭接时我们不修改项目, 正确的功能是数组. prototype. filter 。

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

从今天(11.4.2022)起,94.08%的互联网用户浏览器支持阵列. prototype. filter。 所以一般地说, 使用是安全的。 但是, IE6 - 8 不支持它。 所以, 如果您的用法案例需要支持这些浏览器, Chris Ferdinanti 做了一个不错的多填。

业绩 业绩业绩 业绩业绩

Array. prototype. filter 对大多数使用的案例来说都是很好的。 但是, 如果您在寻找高级数据处理的性能改进, 您可以探索一些其他选项, 比如使用纯的选项。 另一个伟大的选项是重新思考您正在处理的阵列是否真的必须如此大。 这或许是一个信号, 即 JavaScript 应该从数据源获得一个减少的处理阵列 。

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

更现代的ECMAScript 2015(原称和谐或ES6)方法。

const items = [1, 2, 3, 4];
const index = 2;

然后:

items.filter((x, i) => i !== index);

弹出 :

[1, 2, 4]

您可以使用 Babel 和多填充服务,以确保浏览器之间有很好的支持。

删除一个数值,使用松放比较,不突变原始数组ES6

/**
 * Removes one instance of `value` from `array`, without mutating the original array. Uses loose comparison.
 *
 * @param {Array} array Array to remove value from
 * @param {*} value Value to remove
 * @returns {Array} Array with `value` removed
 */
export function arrayRemove(array, value) {
    for(let i=0; i<array.length; ++i) {
        if(array[i] == value) {
            let copy = [...array];
            copy.splice(i, 1);
            return copy;
        }
    }
    return array;
}

您在数组中有 1 到 9 个, 您想要删除 5 个 。 请使用以下代码 :

var number Array = [1、2、3、4、5、6、7、8、9]; var new numberArray = number Array. filter (m { return m!= 5;}); 控制台.log ("新阵列,5 develop",新的数字阵列);


如果您想要多个值。例如:- 1,7,8

var number Array = [1、2、3、4、5、6、7、8、9]; var new numberArray = number Array. filter (m {{{{ {返回} (m = = == 1) {(m = = = 7) {(m = = 7) { (m = = = = = 7;} 控制台.log ("新阵列,1,7和8 移除",新的数字阵列);


如果您想要删除数组中的数组值。 例如 : [3,4,5]

var number Array = [1、2、3、4、5、6、7、8、9]; var remoble Array = [3、4、5、5、6、7、8、9]; var new number Array = number Array. filter (m { { return $remobleAry. includs(m) } 控制台.log ("新阵列, [3、4、5] 移除" , 新的数字阵列);

包括支持的浏览器为链接 。

根据索引删除

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

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