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

array.remove(value);

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


当前回答

根据索引删除

函数返回在索引中没有元素的数组副本 :

/**
* removeByIndex
* @param {Array} array
* @param {Number} index
*/
function removeByIndex(array, index){
      return array.filter(function(elem, _index){
          return index != _index;
    });
}
l = [1,3,4,5,6,7];
console.log(removeByIndex(l, 1));

$> [ 1, 4, 5, 6, 7 ]

以值删除

函数返回没有值的数组副本。

/**
* removeByValue
* @param {Array} array
* @param {Number} value
*/
function removeByValue(array, value){
      return array.filter(function(elem, _index){
          return value != elem;
    });
}
l = [1,3,4,5,6,7];
console.log(removeByValue(l, 5));

$> [ 1, 3, 4, 6, 7]

其他回答

减少方法的利润如下:

(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] : []), []);
}

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

您可以从数组中添加一个原型函数来“ 移除” 元素 。

以下示例显示当我们知道一个元素的索引时如何简单地从数组中删除一个元素。Array.filter方法。

Array.prototype.removeByIndex = function(i) {
    if(!Number.isInteger(i) || i < 0) {
        // i must be an integer
        return this;
    }

    return this.filter((f, indx) => indx !== i)
}
var a = [5, -89, (2 * 2), "some string", null, false, undefined, 20, null, 5];

var b = a.removeByIndex(2);
console.log(a);
console.log(b);

有时候我们不知道元素的索引

Array.prototype.remove = function(i) {
    return this.filter(f => f !== i)
}
var a = [5, -89, (2 * 2), "some string", null, false, undefined, 20, null, 5];

var b = a.remove(5).remove(null);
console.log(a);
console.log(b);

// It removes all occurrences of searched value

但是,当我们只想要删除搜索值的首次出现时,我们就可以使用Array.indexOf函数中的方法。

Array.prototype.removeFirst = function(i) {
    i = this.indexOf(i);

    if(!Number.isInteger(i) || i < 0) {
        return this;
    }

    return this.filter((f, indx) => indx !== i)
}
var a = [5, -89, (2 * 2), "some string", null, false, undefined, 20, null, 5];

var b = a.removeFirst(5).removeFirst(null);
console.log(a);
console.log(b);

删除单个元素

function removeSingle(array, element) {
    const index = array.indexOf(element)
    if (index >= 0) {
        array.splice(index, 1)
    }
}

删除多个元素, 位置内

这对于确保算法在O(N)时间运行更为复杂。

function removeAll(array, element) {
    let newLength = 0
    for (const elem of array) {
        if (elem !== number) {
            array[newLength++] = elem
        }
    }
    array.length = newLength
}

删除多个元素,创建新对象

array.filter(elem => elem !== number)

例如,您有一个字符数组,想要从数组中删除“A”。

数组有一个过滤方法,可以过滤并只返回您想要的元素。

let CharacterArray = ['N', 'B', 'A'];

我想返回元素 除了"A"。

CharacterArray = CharacterArray.filter(character => character !== 'A');

字符阵列必须是 : ['N', 'B']

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