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

array.remove(value);

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


当前回答

我找到了这个博客文章,

9 从 JavaScript 阵列中删除元素的方法 - 附加如何安全清除 JavaScript 阵列

我更喜欢用filter():

var filtered_arr = arr.filter(function(ele){
   return ele != value;
})

其他回答

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

var index = jQuery.inArray(val, arr);
if (index > -1) {
    arr.splice(index, 1);
    //console.log(arr);
}
var index,
    input = [1,2,3],
    indexToRemove = 1;
    integers = [];

for (index in input) {
    if (input.hasOwnProperty(index)) {
        if (index !== indexToRemove) {
            integers.push(result); 
        }
    }
}
input = integers;

此解决方案将需要一系列输入, 并将通过输入查找要删除的值。 这将在整个输入数组中循环, 结果将是第二个已经删除了特定索引的数组整数组。 然后将整数组复制到输入数组中 。

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

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"]

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

从字符串阵列中查找和删除一个特定的字符串:

var colors = ["red","blue","car","green"];
var carIndex = colors.indexOf("car"); // Get "car" index
// Remove car from the colors array
colors.splice(carIndex, 1); // colors = ["red", "blue", "green"]

资料来源:https://www.codegrepper.com/?search_term=remove+a+particular+element+from+array

强调.js可用于解决多个浏览器的问题。 如果存在的话, 它会使用在建浏览器的方法。 如果像旧版的互联网探索者一样缺少这些方法, 它会使用自己的自定义方法 。

从数组中删除元素( 从网站) 的简单示例 :

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]