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

array.remove(value);

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


当前回答

我认为许多 JavaScript 指令对于功能性编程没有很好的想法。 Splice 返回被删除的元素, 大部分时间您需要减少的数组。 这是不好的 。

想象一下您正在做一个循环调用, 并且不得不通过一个阵列, 并用一个更少的项目, 可能没有当前索引化的项目 。 或者想象一下您正在做另一个循环调用, 并且不得不通过一个带有元素推动的阵列 。

在这两种情况下,你都不能做myRecursiveFunction(myArr.push(c))myRecursiveFunction(myArr.splice(i,1))。第一个白痴实际上会通过阵列的长度,第二个白痴将把删除的元素作为参数通过。

所以事实上我所做的是... :删除一个阵列元素, 并将结果传递到一个参数的函数中, 同时我做如下:

myRecursiveFunction(myArr.slice(0,i).concat(a.slice(i+1)))

说到推,那更傻...

myRecursiveFunction((myArr.push(c),myArr))

我相信一种正确的功能语言 一种方法突变它所呼吁的物体 就必须返回一个引用 作为结果的物体本身。

其他回答

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

arr.splice(0,1)

console.log(arr)

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

2017-05-08

大多数给定的回答都用于严格的比较, 意思是两个对象在内存( 或原始类型) 中引用完全相同的对象, 但通常您想要从具有一定值的数组中删除一个非原始对象。 例如, 如果您给服务器打电话, 并想要对照本地对象检查已检索到的对象 。

const a = {'field': 2} // Non-primitive object
const b = {'field': 2} // Non-primitive object with same value
const c = a            // Non-primitive object that reference the same object as "a"

assert(a !== b) // Don't reference the same item, but have same value
assert(a === c) // Do reference the same item, and have same value (naturally)

//Note: there are many alternative implementations for valuesAreEqual
function valuesAreEqual (x, y) {
   return  JSON.stringify(x) === JSON.stringify(y)
}


//filter will delete false values
//Thus, we want to return "false" if the item
// we want to delete is equal to the item in the array
function removeFromArray(arr, toDelete){
    return arr.filter(target => {return !valuesAreEqual(toDelete, target)})
}

const exampleArray = [a, b, b, c, a, {'field': 2}, {'field': 90}];
const resultArray = removeFromArray(exampleArray, a);

//resultArray = [{'field':90}]

数值AreEqual有替代/更快的操作,但这样可以操作。如果您有特定的字段要检查,也可以使用自定义的比较器(例如,有些已检索的 UUID 相对于本地的 UUID ) 。

2. 还注意到这是一个功能操作,意即它不改变原始阵列。

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

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

然后:

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

弹出 :

[1, 2, 4]

您可以使用贝贝以及 a 和 a多元填充服务以确保在浏览器之间得到很好的支持。

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

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

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

  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)