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

array.remove(value);

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


当前回答

我也碰巧遇到这样的情况 我不得不把一个元素从Array. .indexOf不是在互联网探索者工作,所以我分享我的工作jQuery.inArray()解决方案 :

var index = jQuery.inArray(val, arr);
if (index > -1) {
    arr.splice(index, 1);
    //console.log(arr);
}

其他回答

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

只删除第一个34年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄、年龄34:

ages.splice(ages.indexOf(34), 1);

或者您可以在全球定义一种方法 :

function remove(array, item){
    let ind = array.indexOf(item);
    if(ind !== -1)
        array.splice(ind, 1);
}

将所有年龄都除去34:

ages = ages.filter(a => a !== 34);

你可以绕过每个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;
}

从数组中删除一个特定元素, 可以在过滤器选项的一行内完成, 并得到所有浏览器的支持 :https://caniuse.com/#search=filter%20array

function removeValueFromArray(array, value) {
    return array.filter(e => e != value)
}

我在这里测试了这个函数 :https://bit.dev/joshk/jotils/remove-value-from-array/~code#test.ts

于2016年10月16日编辑

  • 做简单、直觉和直观的(a)项(b)项(b)目(c)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f)目(f):奥卡姆剃刀)
  • 执行不可更改( 原始数组保持不变)
  • 用标准的 JavaScript 函数执行, 如果您的浏览器不支持这些函数-使用多填

在此代码示例中,我使用array.filter(...)函数从数组中删除不想要的项目。此函数不改变原始数组,而创建一个新数组。如果浏览器不支持此函数(例如,在版本9之前的 Internet Explorerer 或1.5版本之前的Firefox),请考虑core-js.

删除项目( ECMA-262 版5 代码 AKA 旧风格 JavaScript)

var value = 3

var arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(function(item) {
    return item !== value
})

console.log(arr)
// [ 1, 2, 4, 5 ]

删除项目( ECMAScript 6 代码)

let value = 3

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => item !== value)

console.log(arr)
// [ 1, 2, 4, 5 ]

重要ECMAScript 6() => {}Internet Explorer 中不支持箭头函数语法,45版前的铬、22版前的Firefox和10版前的Safari 。 要在旧浏览器中使用 ECMAScript 6 语法巴比尔JS.


删除多个项目( ECMAScript 7 代码)

此方法的另一个好处是,您可以删除多个项

let forDeletion = [2, 3, 5]

let arr = [1, 2, 3, 4, 5, 3]

arr = arr.filter(item => !forDeletion.includes(item))
// !!! Read below about array.includes(...) support !!!

console.log(arr)
// [ 1, 4 ]

重要 array.includes(...)在互联网探索器、47版本前铬、43版本前Firefox、9版本前Safari、14版本前边缘中,完全不支持互联网探索器、47版本前铬、43版本前Firefox、9版本前Safari、14版本前边缘等功能,但您可以多元填充core-js.

删除多个项目( 未来, 也许)

如果"这个暗暗的语法"提案被接受过,你可以做到这一点:

// array-lib.js

export function remove(...forDeletion) {
    return this.filter(item => !forDeletion.includes(item))
}

// main.js

import { remove } from './array-lib.js'

let arr = [1, 2, 3, 4, 5, 3]

// :: This-Binding Syntax Proposal
// using "remove" function as "virtual method"
// without extending Array.prototype
arr = arr::remove(2, 3, 5)

console.log(arr)
// [ 1, 4 ]

尝试自己在 BabelJS :)

参考参考参考参考参考文献