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

array.remove(value);

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


当前回答

使用过滤过滤器这是达到这一要求的优雅方式。filter将不改变原始数组。

const num = 3;
let arr = [1, 2, 3, 4];
const arr2 = arr.filter(x => x !== num);
console.log(arr); // [1, 2, 3, 4]
console.log(arr2); // [1, 2, 4]

您可以使用filter然后将结果指定为原始数组,如果你想实现突变清除行为的话。

const num = 3;
let arr = [1, 2, 3, 4];
arr = arr.filter(x => x !== num);
console.log(arr); // [1, 2, 4]

顺便说一句,filter将删除在条件中匹配的所有事件( 不只是第一次发生) , 正如您在下面的示例中看到的一样

const num = 3;
let arr = [1, 2, 3, 3, 3, 4];
arr = arr.filter(x => x !== num);
console.log(arr); // [1, 2, 4]

万一您只想删除第一种情况,您可以使用复盘方法

const num = 3;
let arr = [1, 2, 3, 3, 3, 4];
const idx = arr.indexOf(num);
arr.splice(idx, idx !== -1 ? 1 : 0);
console.log(arr); // [1, 2, 3, 3, 4]

其他回答

从数组中删除一个特定元素/字符串可在单班条中进行:

theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);

此处:

阵列:要从

将字符串从矩阵中移除:要删除的字符串,而1是要删除的元素数量。

注 注 注 注 注注:如果“字符串要从阵列中移除”不位于数组中,这将删除数组的最后一个元素。

在移除该元素之前先检查该元素是否存在于您的阵列中, 总是很好的做法 。

if (theArray.indexOf("stringToRemoveFromArray") >= 0){
   theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);
}

取决于客户电脑上是否有新版或旧版的剪贴条:

var array=['1','2','3','4','5','6']
var newArray = array.filter((value)=>value!='3');

var array = ['1','2','3','4','5','6'];
var newArray = array.filter(function(item){ return item !== '3' });

“ 3” 是您想要从数组中删除的值。 然后, 数组将变成 :['1','2','4','5','6']

您可以使用标准__proto__JavaScript 和定义此函数。例如,

let data = [];
data.__proto__.remove = (n) => { data = data.flatMap((v) => { return v !== n ? v : []; }) };

data = [1, 2, 3];
data.remove(2);
console.log(data); // [1,3]

data = ['a','b','c'];
data.remove('b');
console.log(data); // [a,c]

理解这一点:

您可以使用 JavaScript 阵列来组合数值, 并在它们上面循环。 数组项目可以以多种方式被添加和删除。 总共有9种方法( 使用其中适合你的任何一种方法) 。 JavaScript 阵列不使用删除方法, 而是有多种方法可以清除数组值 。

这样做的不同方法:

您可以以这些方式从 JavaScript 阵列中删除元素 :

1- 流行 :从矩阵结尾处删除 。

2-轮班:从数组的开头删除。

3 - Splice: 3 - splice: 3 - splice: 3 - splice: 3 - splice:从特定的矩阵索引中删除。

4- 过滤器 :这将允许您从阵列中从程序上删除元素 。


方法1:从 JavaScript 队列起始时删除元素

var ar = ['zero', 'one', 'two', 'three'];
    
ar.shift(); // returns "zero"
    
console.log( ar ); // ["one", "two", "three"]

方法2:从 JavaScript 阵列结尾删除元素

var ar = [1, 2, 3, 4, 5, 6];
    
ar.length = 4; // set length to remove elements
console.log( ar ); // [1, 2, 3, 4]


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

方法3:在 JavaScript 中使用重复来移除队列元素

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var removed = arr.splice(2,2);
console.log(arr);


var list = ["bar", "baz", "foo", "qux"];
    
list.splice(0, 2); 
console.log(list);

方法4:使用 Splice 依据值删除队列项目

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    
    for( var i = 0; i < arr.length; i++){ 
    
        if ( arr[i] === 5) { 
    
            arr.splice(i, 1); 
        }
    
    }
    
    //=> [1, 2, 3, 4, 6, 7, 8, 9, 10]
    
    
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 10];
    
    for( var i = 0; i < arr.length; i++){ 
                                   
        if ( arr[i] === 5) { 
            arr.splice(i, 1); 
            i--; 
        }
    }

    //=> [1, 2, 3, 4, 6, 7, 8, 9, 10]

方法5:使用队列过滤器方法按值删除项目

  var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    var filtered = array.filter(function(value, index, arr){ 
        return value > 5;
    });
    //filtered => [6, 7, 8, 9]
    //array => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

方法6:Lodash 队列删除方法

var array = [1, 2, 3, 4];
var evens = _.remove(array, function(n) { 
                  return n % 2 === 0;
            });
            
console.log(array);// => [1, 3]console.log(evens);// => [2, 4]

方法7:制作删除方法

var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

function arrayRemove(arr, value) { 
    
        return arr.filter(function(ele){ 
            return ele != value; 
        });
    }
    
var result = arrayRemove(array, 6); // result = [1, 2, 3, 4, 5, 7, 8, 9, 10]

方法8:使用删除运算符明确删除队列元素

var ar = [1, 2, 3, 4, 5, 6];
    
delete ar[4]; // delete element with index 4
    
console.log( ar ); // [1, 2, 3, 4, undefined, 6]
    
alert( ar ); // 1,2,3,4,,6

方法9:清除或重置 JavaScript 阵列

var ar = [1, 2, 3, 4, 5, 6];
//do stuffar = [];
//a new, empty array!

var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1; 
    // Reference arr1 by another variable arr1 = [];
    
console.log(arr2); 
// Output [1, 2, 3, 4, 5, 6]

var arr1 = [1, 2, 3, 4, 5, 6];
var arr2 = arr1; 
    // Reference arr1 by another variable arr1 = [];
    
console.log(arr2); 
// Output [1, 2, 3, 4, 5, 6]

摘要:

关键是要通过删除 JavaScript 矩阵项目来管理您的数据。 虽然没有单一的“ 撤销” 功能, 但您可以使用各种方式和战略清除不必要的阵列元素 。

咖啡冲咖啡:

my_array.splice(idx, 1) for ele, idx in my_array when ele is this_value

Vanilla JavaScript(ES5.1) - (ES5.1) -已经到位版本版本

浏览器支持 :因特网探索者 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) - (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);
}

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