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

array.remove(value);

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


当前回答

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

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

纠正我,如果我错了, 但我们不能假设在关键值地图中, 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 )

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

其他回答

使用 indexof 查找要删除的数组元素的索引,然后用复数删除该索引。

组合法通过删除现有元素和/或添加新元素来改变数组的内容。

const 数组 = [2, 5, 9] ; 控制台. log( 数组); const 指数 = 数组. indexof (5); 如果 (index > - 1) {/ / 仅当发现项目时 复数组 。 spice (index, 1) ; / 2nd 参数意味着只删除一个项 } // 数组 = [2, 9] 控制台. log( 数组) ;

复数的第二个参数是要删除的元素数量。 请注意, 复数会修改所在的数组, 并返回包含已删除元素的新数组 。


由于完整性的原因,此处为函数。第一个函数只删除一个单一事件(即从 [2,5,9,1,5,5,8,5] 中删除第一个匹配5),而第二个函数删除所有事件:

{ var indexof( val值); 如果 (index > - 1 ) { rr. spice( index, 1); } 返回 arr; } 函数删除TroitAll( arr, val值 = 0); { (i) < arr. long) { (arr) {( i) { rr. spice( i, 1); { other { +gi; } } 返回 arr; } / 使用控制台. log( removeteTunce ([ 2, 5, 9, 1, 5, 5, 5, 5 5 ) 控制台. log( removete TempallAll ( 2, 5, 9, 1, 5, 5, 5, 5) ) )

在类型Script中,这些函数可用类型参数保持类型安全:

function removeItem<T>(arr: Array<T>, value: T): Array<T> { 
  const index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}

除了所有这些解决方案之外, 它也可以用阵列来完成. 减量...

const removeItem = 
    idx => 
    arr => 
    arr.reduce((acc, a, i) =>  idx === i ? acc : acc.concat(a), [])

const array = [1, 2, 3]
const index = 1

const newArray = removeItem(index)(array) 

console.log(newArray) // logs the following array to the console : [1, 3]

...或者一个循环函数(诚实地说不是那么优雅...也许有人有更好的循环解决方案? ? )...

const removeItemPrep = 
    acc => 
    i => 
    idx => 
    arr => 

    // If the index equals i, just feed in the unchanged accumulator(acc) else...
    i === idx ? removeItemPrep(acc)(i + 1)(idx)(arr) :

    // If the array length + 1 of the accumulator is smaller than the array length of the original array concatenate the array element at index i else... 
    acc.length + 1 < arr.length ? removeItemPrep(acc.concat(arr[i]))(i + 1)(idx)(arr) : 

    // return the accumulator
    acc 

const removeItem = removeItemPrep([])(0)

const array = [1, 2, 3]
const index = 1

const newArray = removeItem(index)(array) 

console.log(newArray) // logs the following array to the console : [1, 3]

ES6且无突变:(2016年10月)

const removeByIndex = (list, index) => [ ...list.slice(0, index), ...list.slice(index + 1) ]; output = removeByIndex([33,22,11,44],1) //=> [33,11,44] console.log(output)

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

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

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

我还有一个从阵列中移除的好办法:

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

http://developmenter.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter http://global_Objects/Aray/