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

array.remove(value);

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


当前回答

删除一个数值,使用松放比较,不突变原始数组ES6

/**
 * Removes one instance of `value` from `array`, without mutating the original array. Uses loose comparison.
 *
 * @param {Array} array Array to remove value from
 * @param {*} value Value to remove
 * @returns {Array} Array with `value` removed
 */
export function arrayRemove(array, value) {
    for(let i=0; i<array.length; ++i) {
        if(array[i] == value) {
            let copy = [...array];
            copy.splice(i, 1);
            return copy;
        }
    }
    return array;
}

其他回答

使用 indexof 查找要删除的数组元素的索引,然后用复数删除该索引。

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

const 数组 = [2, 5, 9] ; 控制台. log( 数组); const 指数 = 数组. indexof (5); 如果 (index > - 1) {/ / 仅当发现项目时 复数组 。 spice (index, 1) ; / 2nd 参数意味着只删除一个项 } // 数组 = [2, 9] 控制台. log( 数组) ;

复数的第二个参数是要删除的元素数量。 请注意, 复数会修改所在的数组, 并返回包含已删除元素的新数组 。


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

{ var indexof( val值); 如果 (index > - 1 ) { rr. spice( index, 1); } 返回 arr; } 函数删除TroitAll( arr, val值 = 0); { (i) < arr. long) { (arr) {( i) { rr. spice( i, 1); { other { +gi; } } 返回 arr; } / 使用控制台. log( removeteTunce ([ 2, 5, 9, 1, 5, 5, 5, 5 5 ) 控制台. log( removete TempallAll ( 2, 5, 9, 1, 5, 5, 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;
}

使用不同种类数组的过滤器

    **simple array**
    const arr = ['1','2','3'];
    const updatedArr = arr.filter(e => e !== '3');
    console.warn(updatedArr);

    **array of object**
    const newArr = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]
    const updatedNewArr = newArr.filter(e => e.id !== 3);
    console.warn(updatedNewArr);

    **array of object with different parameter name**
    const newArr = [{SINGLE_MDMC:{id:1,cout:10}},{BULK_MDMC:{id:1,cout:15}},{WPS_MDMC:{id:2,cout:10}},]
    const newArray = newArr.filter((item) => !Object.keys(item).includes('SINGLE_MDMC'));
    console.log(newArray)

我还有一个从阵列中移除的好办法:

var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]

http://developmenter.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter http://global_Objects/Aray/

我不知道你如何期待阵列.remove( int) 的行为。 我可以想到三种可能性, 你可能会想要。

要删除索引 i 中的数组元素:

array.splice(i, 1);

如果您想要从数组中删除带有数值数的每个元素 :

for (var i = array.length - 1; i >= 0; i--) {
 if (array[i] === number) {
  array.splice(i, 1);
 }
}

如果您只想在索引 I 中使元素不再存在, 但是您不想让其它元素的索引改变 :

delete array[i];

您可以使用 lodash _. pull( 调和数组)、 _. pullAt( 调和数组) 或 _. 。 (不变换数组)

var array1 = ['a', 'b', 'c', 'd']
_.pull(array1, 'c')
console.log(array1) // ['a', 'b', 'd']

var array2 = ['e', 'f', 'g', 'h']
_.pullAt(array2, 0)
console.log(array2) // ['f', 'g', 'h']

var array3 = ['i', 'j', 'k', 'l']
var newArray = _.without(array3, 'i') // ['j', 'k', 'l']
console.log(array3) // ['i', 'j', 'k', 'l']