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

array.remove(value);

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


当前回答

您可以简单地将删除函数添加到数组原体Type

类似的东西 :

Array.prototype.remove = function(value) {
  return  this.filter(item => item !== value)
}

Array.prototype.remove = function(value) {
  return  this.filter(item => item !== value)
}

let array = [10,15,20]

result = array.remove(10)
console.log(result)

其他回答

你可以绕过每个array- 分项目和项目splice如果它存在于你的体内array.

function destroy(arr, val) {
    for (var i = 0; i < arr.length; i++) if (arr[i] === val) arr.splice(i, 1);
    return arr;
}

使用 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
>

我建议删除一个使用删除和过滤的阵列项目:

var arr = [1,2,3,4,5,5,6,7,8,9];
delete arr[5];
arr = arr.filter(function(item){ return item != undefined; });
//result: [1,2,3,4,5,6,7,8,9]

console.log(arr)

因此,我们只能删除一个特定的数组项目,而不是所有具有相同价值的项目。

您可以使用Set,然后使用delete函数 :

const s = Set;
s.add('hello');
s.add('goodbye');
s.delete('hello');

ES1010

该员额汇总了截至2001年1月1日从数组中删除元素的通用方法ECMAScript 2019(ES10)。

1. 普通案件

1.1. 使用数值删除矩阵元素.splice()

| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 是( loop) , 否( indexof)
| 按值/ 指数:按指数:___________________________________________________________________________________________________________________________________________________________________________________

如果您知道要从数组中删除的值, 您可以使用复数法。 首先, 您必须确定目标项的索引。 然后使用索引作为起始元素, 并删除一个元素 。

// With a 'for' loop
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( let i = 0; i < arr.length; i++){
  if ( arr[i] === 5) {
    arr.splice(i, 1);
  }
} // => [1, 2, 3, 4, 6, 7, 8, 9, 0]

// With the .indexOf() method
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
const i = arr.indexOf(5);
arr.splice(i, 1); // => [1, 2, 3, 4, 6, 7, 8, 9, 0]

1.2. 使用.filter()方法

| 在职: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数: 按数值 :%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

具体要素可以是已经过滤从数组中,通过提供过滤功能,从数组中取出。然后对数组中的每个元素要求使用此函数。

const value = 3
let arr = [1, 2, 3, 4, 5, 3]
arr = arr.filter(item => item !== value)
console.log(arr)
// [ 1, 2, 4, 5 ]

1.3. 通过扩展来删除队列元素Array.prototype

| 在职:是/否(取决于执行情况)__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:是/否(取决于执行情况)__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:按指数 / 按数值(视执行情况而定)

阵列原型可以通过其他方法扩展,然后在创建的阵列上使用这些方法。

注:一些人认为,从JavaScript标准图书馆(如Array)扩展物体原型是反型。

// In-place, removes all, by value implementation
Array.prototype.remove = function(item) {
    for (let i = 0; i < this.length; i++) {
        if (this[i] === item) {
            this.splice(i, 1);
        }
    }
}
const arr1 = [1,2,3,1];
arr1.remove(1) // arr1 equals [2,3]

// Non-stationary, removes first, by value implementation
Array.prototype.remove = function(item) {
    const arr = this.slice();
    for (let i = 0; i < this.length; i++) {
        if (arr[i] === item) {
            arr.splice(i, 1);
            return arr;
        }
    }
    return arr;
}
let arr2 = [1,2,3,1];
arr2 = arr2.remove(1) // arr2 equals [2,3,1]

1.4. 使用delete操作员

| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:按指数:___________________________________________________________________________________________________________________________________________________________________________________

使用删除运算符不会影响长度属性。 它也不会影响后续元素的索引。 数组变得稀少, 这是一种巧妙的方式, 表示删除的项目没有被删除, 但却没有定义 。

const arr = [1, 2, 3, 4, 5, 6];
delete arr[4]; // Delete element with index 4
console.log( arr ); // [1, 2, 3, 4, undefined, 6]

删除运算符的设计是为了从 JavaScript 对象中删除属性,而数组则是对象。

1.5. 使用Object公用事业(ES10)

| 在职: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数: 按数值 :%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

A. 实行ES-1010Object.fromEntries,可以用来从任何类似阵列的物体中创建所需的阵列,并在过程中过滤不想要的元素。

const object = [1,2,3,4];
const valueToRemove = 3;
const arr = Object.values(Object.fromEntries(
  Object.entries(object)
  .filter(([ key, val ]) => val !== valueToRemove)
));
console.log(arr); // [1,2,4]

2. 特殊情况

2.1 如果在阵列末端,则删除元素

2.1.1. 变化阵列length

| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________

JavaScript 阵列元素可以通过将长度属性设定为低于当前值的值,从数组的末尾删除。任何指数大于或等于新长度的元素将被删除 。

const arr = [1, 2, 3, 4, 5, 6];
arr.length = 5; // Set length to remove element
console.log( arr ); // [1, 2, 3, 4, 5]

2.1.2. 使用.pop()方法

| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________

流行方法删除数组最后一个元素, 返回该元素, 并更新长度属性。 流行方法修改援引该元素的数组, 这意味着与使用删除最后一个元素的方式不同, 将完全删除最后一个元素, 并缩小数组长度 。

const arr = [1, 2, 3, 4, 5, 6];
arr.pop(); // returns 6
console.log( arr ); // [1, 2, 3, 4, 5]

2.2. 如果在阵列开始时,则删除元素

| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复: 否 ____________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________

缩略.shift()方法与流行法非常相似, 但它删除了 JavaScript 数组的第一个元素, 而不是最后一个元素。 当元素被删除时, 其余元素会被移到下方 。

const arr = [1, 2, 3, 4];
arr.shift(); // returns 1
console.log( arr ); // [2, 3, 4]

2.3. 删除元素,如果它是阵列中唯一的元素

| 在职:是________________________________________________________________________________________________________________________________________________________________________________________________________
| 删除重复:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________
| 按值/ 指数:不适用_______________________________________________________________________________________________________________________________________________________________________________________________________________________

最快的技术是设置空数组的数组变量。

let arr = [1];
arr = []; //empty array

2.1.1的替代技术可通过将长度设定为0来使用。