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

array.remove(value);

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


当前回答

从字符串阵列中查找和删除一个特定的字符串:

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car"); // Get "car" index
// Remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red", "blue", "green"]

资料来源:https://www.codegrepper.com/?search_term=remove+a+particulate+element+ from+array? 来源:https://www.codegrepper.com/?search_term=remove+a+partical+element+ from+array

其他回答

var index,
    input = [1,2,3],
    indexToRemove = 1;
    integers = [];

for (index in input) {
    if (input.hasOwnProperty(index)) {
        if (index !== indexToRemove) {
            integers.push(result); 
        }
    }
}
input = integers;

此解决方案将需要一系列输入, 并将通过输入查找要删除的值。 这将在整个输入数组中循环, 结果将是第二个已经删除了特定索引的数组整数组。 然后将整数组复制到输入数组中 。

我测试了复数和过滤器 看哪个更快:

let someArr = [... Array(999999). keys ()] 控制台.time(“ filter” ) someArr. filter(x x = = = = = 666666) 控制台.timeEnd(“ filter ” ) 控制台.time(“ splice by indexof ” ) someArr. splice( some Arr. indexof( 6666), 1) 控制台.timeEnd( “ splice by indexof ” )

在我的机器上, 复数会更快。 这有道理, 因为复数只编辑一个已有的数组, 而过滤器会创建一个新的数组 。

因此,过滤器在逻辑上是更清洁的(更容易阅读),并且更适合使用不可改变状态的编码风格。因此,由你来决定你是否想做出这种权衡。

您可以用所有存取器示例创建索引 :

<div >
</div>

function getIndex($id){ return ( this.removeIndex($id) alert("This element was removed") ) } function removeIndex(){ const index = $id; this.accesor.id.splice(index.id) // You can use splice for slice index on // accessor id and return with message } <div> <fromList> <ul> {...this.array.map( accesors => { <li type="hidden"></li> <li>{...accesors}</li> }) } </ul> </fromList> <form id="form" method="post"> <input id="{this.accesors.id}"> <input type="submit" callbackforApplySend...getIndex({this.accesors.id}) name="sendendform" value="removeIndex" > </form> </div>

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

我喜欢这个单行道:

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

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