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

array.remove(value);

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


当前回答

 const Delete = (id) => {
      console.log(id)
     var index = Array.map(function(e){
      return e.id;
     
     }).indexOf(id);
     Array.splice(index,1);
     
}

其他回答

如果您想要删除删除位置的新数组,您可以总是删除特定元素,并过滤该数组。它可能需要扩展矩阵对象对于不执行过滤法的浏览器来说,但从长远来看,它更容易,因为你所做的只是这个:

var my_array = [1, 2, 3, 4, 5, 6];
delete my_array[4];
console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));

它应显示[1, 2, 3, 4, 6].

ES6且无突变:(2016年10月)

const removeByIndex = (list, index) =>
      [
        ...list.slice(0, index),
        ...list.slice(index + 1)
      ];
         
output = removeByIndex([33,22,11,44],1) //=> [33,11,44]
      
console.log(output)

从数组中删除一个特定元素, 可以在过滤器选项的一行内完成, 并得到所有浏览器的支持 :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

您永远不应该根据功能性编程模式改变您的阵列。 您可以创建一个新的阵列, 而不引用您想要更改的数据 ECMAScript 6 方法 。filter;

var myArray = [1, 2, 3, 4, 5, 6];

假设您想要删除5从数组中,您可以简单地这样做:

myArray = myArray.filter(value => value !== 5);

这将给您一个没有您想要删除的值的新数组。 因此结果将是 :

 [1, 2, 3, 4, 6]; // 5 has been removed from this array

关于进一步理解,请阅读MDN文件:Array.过滤器.

最简单的方法可能是使用过滤功能。例如:

let array = ["hello", "world"]
let newarray = array.filter(item => item !== "hello");
console.log(newarray);
// ["world"]