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

array.remove(value);

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


当前回答

快速解答和回答: 使用过滤器或拼盘方法 。

在此读取文档 :

过滤法:https://developmenter.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter Splice方法:https://developter.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/spice

其他回答

删除在索引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);
}

使用 JavaScript 原型特性定义列对象上名为删除() 的方法 。

使用 spolice () 方法满足您的要求 。

请看看下面的代码

Array.prototype.remove = function(item) {
    // 'index' will have -1 if 'item' does not exist,
    // else it will have the index of the first item found in the array
    var index = this.indexOf(item);

    if (index > -1) {
        // The splice() method is used to add/remove items(s) in the array
        this.splice(index, 1);
    }
    return index;
}

var arr = [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];

// Printing array
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)

// Removing 67 (getting its index, i.e. 2)
console.log("Removing 67")
var index = arr.remove(67)

if (index > 0){
    console.log("Item 67 found at ", index)
} else {
    console.log("Item 67 does not exist in array")
}

// Printing updated array
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)

// ............... Output ................................
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]
// Removing 67
// Item 67 found at  2
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]

注:以下是Norde.js REPL上执行的完整示例代码,该代码描述推()、流行()、转移()、非轮档()和交点()方法的使用。

> // Defining an array
undefined
> var arr = [12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34];
undefined
> // Getting length of array
undefined
> arr.length;
16
> // Adding 1 more item at the end i.e. pushing an item
undefined
> arr.push(55);
17
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34, 55 ]
> // Popping item from array (i.e. from end)
undefined
> arr.pop()
55
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Remove item from beginning
undefined
> arr.shift()
12
> arr
[ 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Add item(s) at beginning
undefined
> arr.unshift(67); // Add 67 at beginning of the array and return number of items in updated/new array
16
> arr
[ 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.unshift(11, 22); // Adding 2 more items at the beginning of array
18
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> // Define a method on array (temporarily) to remove an item and return the index of removed item; if it is found else return -1
undefined
> Array.prototype.remove = function(item) {
... var index = this.indexOf(item);
... if (index > -1) {
..... this.splice(index, 1); // splice() method is used to add/remove items in array
..... }
... return index;
... }
[Function]
>
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(45);    // Remove 45 (you will get the index of removed item)
3
> arr
[ 11, 22, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(22)    // Remove 22
1
> arr
[ 11, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.remove(67)    // Remove 67
1
> arr
[ 11, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(89)    // Remove 89
2
> arr
[ 11, 67, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(100);  // 100 doesn't exist, remove() will return -1
-1
>

业绩 业绩业绩 业绩业绩

今天(2019-12-09),我为选定的解决方案对macOS v10.13.6(高塞拉利昂)进行性能测试。我表示删除(A),但与其他方法相比,我没有使用,因为它在阵列中留下了空位。

结 结 结 结 结 结 结

最快的溶液是阵列。 splice (C) (对于大阵列、 阵列、 slice+splice (H) 是 Firefox 和 Safari 最快的不可改变的溶液 。 来自 (B) 的 Array. forest in Chrome mable 溶液通常比位于Safari 的小型表格的不可移动的快1.5x-6x, 令人惊讶的是, 变式溶(C) 慢于不可改变的溶液 (G) 。

详细细节

在测试中,我以不同的方式将中间元素从数组中去除。 A, C 的解决方案就位。 B, D, E, F, G, H 的解决方案是不可改变的 。

包含 10 元素的阵列结果

在铬中, 数组. spice (C) 是本地最快的解决方案。 数组. filter (D) 是最快的不可变的解决方案。 最慢的就是数组. sice (F)。 您可以在这里对您的机器进行测试 。

带有 1 000 000 元素的阵列结果

在铬中, 数组. spice (C) 是本地最快的解决方案( 删除( C) 的速率相似) - 但是在数组中留下了一个空位( 所以它不会执行“ 完全移除 ” ) 。 数组. sice- splice (H) 是最快的不可改变的解决方案。 最慢的就是数组. filter (D和E) 。 您可以在这里对您的机器进行测试 。

[var a = [0, 1, 2, 3, 4, 5, 6, 7, 9];var log = (letter, array) {(letter, trara.) log.log (leg. join `, `); 函数 A(arry) {var = trares[index]; {var (arr) {var = Array.(ar) 5; var. spice (al) = (aly) log (b) ; {var.log (ar) = listal. g(ary) = listal(5); trace. sloa. (var) log (var) = a. (l) a. (l) ; (var) lix (l) a. (l) a. (l) a. (l) ; (l) a. (l) a. (l) a. (l) a. (l) ex. (l) ex. (l) a. (l) a. (l) a. (l) a. (l) a. (l) a.

浏览器比较:Chrome v78.0.0,Safari v.13.0.4和Firefox v71.0.0

多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。

我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?

纠正我,如果我错了, 但我们不能假设在关键值地图中, JavaScript 物体的种类就是 JavaScript 对象, 关键检索机制可以被假定为高度工程化和优化? (NB: 如果一些超级专家告诉我情况并非如此, 我建议使用ECMAScript 6 的地图类, 这当然会是这样 ) 。

我只是建议,在某些情况下,最好的解决方案可能是将您的阵列转换为对象... 当然,问题在于您可能有重复的整数值。 我建议将那些放在桶中作为关键 {} {} 值条目中的“ 价值” 部分。 (NB: 如果您确定您没有重复的阵列元素, 这样可以简单得多 : 值“ 和” 键, 只需去对象. values (...) 就可以返回修改后的阵列 。)

所以,你可以做到这一点:

const arr = [ 1, 2, 55, 3, 2, 4, 55 ];
const f =    function( acc, val, currIndex ){
    // We have not seen this value before: make a bucket... NB: although val's typeof is 'number',
    // there is seamless equivalence between the object key (always string)
    // and this variable val.
    ! ( val in acc ) ? acc[ val ] = []: 0;
    // Drop another array index in the bucket
    acc[ val ].push( currIndex );
    return acc;
}
const myIntsMapObj = arr.reduce( f, {});

console.log( myIntsMapObj );

产出:

对象 [<1个空位>、Array1、Array[2]、Array1、Array1、<5个空位]、46个以上...]

然后很容易删除所有55个数字。

delete myIntsMapObj[ 55 ]; // Again, although keys are strings this works

你不必全部删除: 指数值按外观被挤进桶里, 所以(例如):

myIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();

将分别删除第一次和最后一次发生的情况。 您可以很容易地计算发生频率, 将一个桶的内装物转移到另一个桶等, 将所有55个都替换为3个 。

从您的“ bucket 对象” 中获取修改的整数组略为涉及, 但数量不多 : 每个桶都包含以( 字符串) 键表示的值的索引( 原始数组) 。 这些桶的每一个值也是独一无二的( 每个是原始数组中唯一的索引值 ) : 所以您把它们转换成新对象中的键, 将“ 整数” 中的( 真实) 整数作为值... 然后对键进行排序, 然后对对象. values (...) 排序 。

这听起来很牵强,很费时... 但显然一切都取决于环境 和理想的用法。我的理解是 JavaScript 的所有版本和背景 只能用一条线运作, 这条线不会“放手 ” , 所以可能会有一些可怕的拥堵 , 使用“强力” 方法: 不是由 Enterof 行动造成的, 而是由多个重复的切片/碎块操作造成的。

增编 如果你确定这对你来说太过分工程了 使用这个案子的话 最简单的"强力"方法肯定就是

const arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )

(是的,其他的答案 已经发现阵列. 原型. 过滤器... )

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

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

在这两种情况中,你都不能做我的递归功能(myArr.push(c))或我的递归功能(myArr.spice(i,1) ) 。 第一个白痴实际上会通过阵列的长度,第二个白痴会通过删除的元素作为参数。

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

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

说到推,那更傻...

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

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