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

array.remove(value);

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


当前回答

Vanilla JavaScript(ES5.1) - 已建立版本

浏览器支持: Internet Explorer 9 或后( 详细浏览器支持)

/**
 * Removes all occurences of the item from the array.
 *
 * Modifies the array “in place”, i.e. the array passed as an argument
 * is modified as opposed to creating a new array. Also returns the modified
 * array for your convenience.
 */
function removeInPlace(array, item) {
    var foundIndex, fromIndex;

    // Look for the item (the item can have multiple indices)
    fromIndex = array.length - 1;
    foundIndex = array.lastIndexOf(item, fromIndex);

    while (foundIndex !== -1) {
        // Remove the item (in place)
        array.splice(foundIndex, 1);

        // Bookkeeping
        fromIndex = foundIndex - 1;
        foundIndex = array.lastIndexOf(item, fromIndex);
    }

    // Return the modified array
    return array;
}

Vanilla JavaScript(ES5.1) - 永恒版本

浏览器支持: 与原版的香草 JavaScript 相同

/**
 * Removes all occurences of the item from the array.
 *
 * Returns a new array with all the items of the original array except
 * the specified item.
 */
function remove(array, item) {
    var arrayCopy;

    arrayCopy = array.slice();

    return removeInPlace(arrayCopy, item);
}

Vanilla ES6 - 永恒版本

浏览器支持: Chrome 46, 边缘 12, Firefox 16, Opera 37, Safari 8 (详细浏览器支持)

/**
 * Removes all occurences of the item from the array.
 *
 * Returns a new array with all the items of the original array except
 * the specified item.
 */
function remove(array, item) {
    // Copy the array
    array = [...array];

    // Look for the item (the item can have multiple indices)
    let fromIndex = array.length - 1;
    let foundIndex = array.lastIndexOf(item, fromIndex);

    while (foundIndex !== -1) {
        // Remove the item by generating a new array without it
        array = [
            ...array.slice(0, foundIndex),
            ...array.slice(foundIndex + 1),
        ];

        // Bookkeeping
        fromIndex = foundIndex - 1;
        foundIndex = array.lastIndexOf(item, fromIndex)
    }

    // Return the new array
    return array;
}

其他回答

ES1010

该员额总结了从ECMAScript 2019(ES10)时的阵列中去除元素的通用方法。

1. 普通案件

1.1. 使用 .spice () 以值清除矩阵元素

| In-place: Yes | | Removes duplicates: Yes(loop), No(indexOf) | | By value / index: By index |

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

// 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 () 方法删除矩阵元素

| In-place: No | | Removes duplicates: Yes | | By value / index: By value |

特定元素可以通过提供过滤功能从数组中过滤出来。然后对数组中的每一元素要求此函数。

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. 通过扩展阵列.prototype来删除阵列元素

现职:是/否(取决于执行情况) 删除重复:是/否(取决于执行情况) 按数值/指数:按指数 / 按数值(取决于执行情况)

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

注:从JavaScript标准图书馆(如阵列)扩展物体原型,被一些人视为反型。

// 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. 使用删除运算符删除队列元素

此处:是 删除重复: 否

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

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. 使用物体功用(ES10)删除阵列元素

| In-place: No | | Removes duplicates: Yes | | By value / index: By value |

从 Entries 引入的 ES10 对象。 可用于在工艺过程中从任何类似 矩阵的物体中创建所需的矩阵和过滤不想要的元素。

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. 改变阵列长度

此处:是 {__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

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. 如果在阵列开始时,则删除元素

此处:是 {__________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________

. toft () 方法与流行方法非常相似, 但它删除了 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来使用。

减少方法的利润如下:

(a) 需要按索引删除某一要素:

function remove(arr, index) {
  return arr.reduce((prev, x, i) => prev.concat(i !== index ? [x] : []), []);
}

b) 需要删除元素值(int)的元素:

function remove(arr, value) {
  return arr.reduce((prev, x, i) => prev.concat(x !== value ? [x] : []), []);
}

这样我们就可以返回一个新的阵列( 将会以酷酷的功能方式- 比使用推或组合要好得多) , 并删除元素 。

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

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

开始

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

删除“ 计算” 选项@ action

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

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

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

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

如需更多参考,请通过:

Array. prototype.spice ()

您可以扩展数组对象以定义自定义的删除函数如下:

let let = [1, 2,4,4,4,5,5,45,9;] 数.delete = 函数(价值) { var indexof Target = this.indexof (value) =.indexof(value) = this.if(indexoftaget = = = = = = = = = = y= = y,4,4,4,5,4,459; 数. 数.delet = 函数(val = val indexof Talget = this. indexeof(valent) = = = = = indexget =.indexof = = =. { = indexexuf(val =) { = = indexete = = / / / imme eleteletelete (1) // / / / / 预期输出= / / / tradestrayf delett 删除 2,4,4,4,4,4,4,5,5,5,4,4,5,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,9/ /

弹出 - 从阵列变化的结尾处删除 - 从阵列复件的开头处删除 - 从特定的阵列索引过滤器中删除 - 允许您程序化地从阵列中删除元素