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

array.remove(value);

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


当前回答

有个朋友有问题因特网探索者 8并告诉我他做了什么。我告诉他这是错的,他告诉我他在这里得到了答案。当前的顶级答案不会在所有浏览器(例如因特网探索者8号)中都有效,它只会消除项目首次出现的情况。

从数组中删除所有实例

function removeAllInstances(arr, item) {
   for (var i = arr.length; i--;) {
     if (arr[i] === item) arr.splice(i, 1);
   }
}

它会从数组向后循环( 因为随着项目被删除, 指数和长度会变化) , 如果找到的话, 它会删除它。 它会在所有浏览器中发挥作用 。

其他回答

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

使用使用复数()满足要求的方法。

请看看下面的代码

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 ]

注:下面是用完整示例代码执行的完整示例代码。REPL编号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
>

我设定了一个函数:

function pop(valuetoremove, myarray) {
    var indexofmyvalue = myarray.indexOf(valuetoremove);
    myarray.splice(indexofmyvalue, 1);
}

并用它这样:

pop(valuetoremove, myarray);

缩略复数()通过删除或替换现有元素和(或)添加新元素来改变数组的内容。

数组。 spice( 开始 [, 删除 [, 删除 [, 项1 [, 项2 [, . ] ] ] ]

开始

开始更改数组的索引( 与源值为 0 ) 。 如果数组的长度大于数组的长度, 则实际起始指数将设置为数组的长度。 如果为负, 则开始从数组结尾处( 与源值为-1) 的许多元素, 如果绝对值大于数组的长度, 则将设置为 0 。

删除“ 计算” 选项@ action

表示要删除的旧数组元素数的整数。

如果删除“计算”被省略,或者如果其值大于数组。 长度 - 开始( 即如果它大于数组中剩下的元素数量, 从开始) , 那么从开始到数组结尾的所有元素都将删除。 如果删除“ 计算” 0 或“ 负” , 则不删除任何元素。 在这种情况下, 您应该至少指定一个新元素( 见下文) 。

第1项,第2项,...可选

要添加到数组的元素, 从起始索引开始。 如果您不指定任何元素, 组合() 只会从数组中删除元素 。

如需更多参考,请通过:

Array. prototype.spice ()

查找index使用indexOf,然后删除该索引splice.

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

const array = [2, 5, 9];

console.log(array);

const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
  array.splice(index, 1); // 2nd parameter means remove one item only
}

// array = [2, 9]
console.log(array); 

第二个参数的第二个参数splice是要删除的元素数。请注意splice修改现有数组,并返回含有已删除元素的新数组。


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

function removeItemOnce(arr, value) {
  var index = arr.indexOf(value);
  if (index > -1) {
    arr.splice(index, 1);
  }
  return arr;
}

function removeItemAll(arr, value) {
  var i = 0;
  while (i < arr.length) {
    if (arr[i] === value) {
      arr.splice(i, 1);
    } else {
      ++i;
    }
  }
  return arr;
}
// Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,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;
}

使用数组过滤过滤器方法 :

let array = [1, 2, 3, 4, 511, 34, 511, 78, 88];

let value = 511;
array = array.filter(element => element !== value);
console.log(array)