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

array.remove(value);

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


当前回答

我本人也有这个问题(在更换阵列是可以接受的情况下),

var filteredItems = this.items.filter(function (i) {
    return i !== item;
});

要给上面的片段略加上下文:

self.thingWithItems = {
    items: [],
    removeItem: function (item) {
        var filteredItems = this.items.filter(function (i) {
            return i !== item;
        });

        this.items = filteredItems;
    }
};

此解决方案应该同时使用引用项和值项。 它都取决于您是否需要保持对原始数组的引用, 以判断该解决方案是否适用 。

其他回答

您可以使用过滤器方法轻松操作 :

函数删除( 收到原件, 元素将移动) { 返回 原件 。 filter( 职能 ) { return el ! === 元素 Toremove} ;} 控制台. log (remove ([ 1, 1, 1, 0, 3, 1, 4], 1 ) ;

这将清除数组中的所有元素, 并且比切片和索引的组合效果更快 。

从带有数组元素的阵列列表中获取阵列和索引 从使用地图toint () 和 Toarray () 方法的阵列新阵列中删除 使用删除 () 方法删除具体索引元素 返回格式化的阵列

您可以使用

Array.splice(index);

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

<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. removeTrounds = 函数( array, val) { { 数组. (raryTrounds, index) {如果 (rayTroups = val) { 数组. splice( index, 1);} 返回数组;} var DummyAray = [1、2、 3、4、 5、 6]; 控制台.log (DummyArray. removeTround (DummyArray, 3) );