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

array.remove(value);

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


当前回答

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

其他回答

  1. 使用 indexof, 能找到来自数组的具体数字索引
    • 使用 spplice, 可以从数组中删除特定的索引 。

const array = [1,2,3,4,5,6,7,8,9,0];
const index = array.indexOf(5);
// find Index of specific number
if(index != -1){
    array.splice(index, 1); // remove number using index
}
console.log(array);

  1. 删除全部事件 。 @ info: whatsthis

let array = [1, 2, 3, 4, 5, 1, 7, 8, 9, 2, 3, 4, 5, 6];
array = array.filter(number=> number !== 5);
console.log(array);

  1. 使用合并和拆分

    let array = [1, 2, 3, 4, 5, 1, 7, 8, 9, 2, 3, 4, 5, 6]
    array = Array.from(array.join("-").split("-5-").join("-").split("-"),Number)
    console.log(array)

var arr =[1,2,3,4,5];

arr.splice(0,1)

console.log(arr)

产出[2、3、4、5];

删除在索引i 上的元素, 不突变原始数组 :

/**
* removeElement
* @param {Array} array
* @param {Number} index
*/
function removeElement(array, index) {
   return Array.from(array).splice(index, 1);
}

// Another way is
function removeElement(array, index) {
   return array.slice(0).splice(index, 1);
}

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

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

    Array.prototype.remove = function(start, end) {
        var n = this.slice((end || start) + 1 || this.length);
        return this.length = start < 0 ? this.length + start : start,
        this.push.apply(this, n)
    }

startend可以是负数。在这种情况下,它们会从数组的末尾计数。

如果只有start中指定,只删除一个元素。

函数返回新数组长度。

z = [0,1,2,3,4,5,6,7,8,9];

newlength = z.remove(2,6);

(8) [0, 1, 7, 8, 9]

z=[0,1,2,3,4,5,6,7,8,9];

newlength = z.remove(-4,-2);

(7) [0, 1, 2, 3, 4, 5, 9]

z=[0,1,2,3,4,5,6,7,8,9];

newlength = z.remove(3,-2);

(4) [0, 1, 2, 9]