如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
你的问题没有说明顺序或不同的价值是否是一项要求。
如果您不关心顺序, 并且容器中的值不会超过一次, 请使用“ Set ” 。 它会更快, 更简洁 。
var aSet = new Set();
aSet.add(1);
aSet.add(2);
aSet.add(3);
aSet.delete(2);
其他回答
如果您使用现代浏览器, 您可以使用. 过滤器 。
Array.prototype.remove = function(x){
return this.filter(function(v){
return v !== x;
});
};
var a = ["a","b","c"];
var b = a.remove('a');
以下是几个方法使用 JavaScript 从数组中删除项目.
描述的所有方法不变换原始数组,而不是创造一个新的。
假设您有一个数组,而您想要删除位置中的项目i
.
一种方法是使用slice()
:
const items = ['a', 'b', 'c', 'd', 'e', 'f']
const i = 3
const filteredItems = items.slice(0, i).concat(items.slice(i+1, items.length))
console.log(filteredItems)
slice()
创建新数组, 使用它收到的索引 。 我们简单地创建一个新数组, 从开始到要删除的索引, 并且将另一个数组从我们删除的后第一个位置集中到数组的末尾 。
在这种情况下,一个好的选择是使用filter()
,它提供了更多宣示方针:
const items = ['a', 'b', 'c', 'd', 'e', 'f']
const valueToRemove = 'c'
const filteredItems = items.filter(item => item !== valueToRemove)
console.log(filteredItems)
此选项使用 ES6 箭头函数。 您可以使用传统函数支持旧的浏览器 :
const items = ['a', 'b', 'c', 'd', 'e', 'f']
const valueToRemove = 'c'
const filteredItems = items.filter(function(item) {
return item !== valueToRemove
})
console.log(filteredItems)
或者你可以使用 Babel 将ES6代码转换回ES5, 使旧浏览器更容易消化, 而在您的代码中写入现代 JavaScript 。
如果不是单项,而是要删除许多项,会怎么样?
让我们找到最简单的解决方案
您可以在序列中创建函数并删除项目 :
const items = ['a', 'b', 'c', 'd', 'e', 'f']
const removeItem = (items, i) =>
items.slice(0, i-1).concat(items.slice(i, items.length))
let filteredItems = removeItem(items, 3)
filteredItems = removeItem(filteredItems, 5)
//["a", "b", "c", "d"]
console.log(filteredItems)
您可以在回溯函数中搜索包含内容 :
const items = ['a', 'b', 'c', 'd', 'e', 'f']
const valuesToRemove = ['c', 'd']
const filteredItems = items.filter(item => !valuesToRemove.includes(item))
// ["a", "b", "e", "f"]
console.log(filteredItems)
splice()
(不与slice()
变换原始数组,并应避免。
(最初张贴在我的网站上)https://flaviocopes.com/how-to-remove-item-from-array/)
你可以轻而易举地用过滤过滤器方法 :
function remove(arrOriginal, elementToRemove){
return arrOriginal.filter(function(el){return el !== elementToRemove});
}
console.log(remove([1, 2, 1, 0, 3, 1, 4], 1));
这将清除数组中的所有元素, 并且运行速度比slice
和indexOf
.
查找index
使用indexOf
,然后删除该索引splice
.
组合法通过删除现有元素和/或添加新元素来改变数组的内容。
const array = [2, 5, 9];
console.log(array);
const index = array.indexOf(5);
if (index > -1) { // only splice array when item is found
array.splice(index, 1); // 2nd parameter means remove one item only
}
// array = [2, 9]
console.log(array);
第二个参数的第二个参数splice
是要删除的元素数。请注意splice
修改现有数组,并返回含有已删除元素的新数组。
由于完整性的原因,此处为函数。第一个函数只删除一个单一事件(即删除第一个匹配5
调自[2,5,9,1,5,8,5]
),而第二个函数删除所有事件:
function removeItemOnce(arr, value) {
var index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
function removeItemAll(arr, value) {
var i = 0;
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
}
// Usage
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,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;
}
对我而言,越简单越好,2018年(2019年左右),我给你这个(近一点)单行话,回答最初的问题:
Array.prototype.remove = function (value) {
return this.filter(f => f != value)
}
有用的是,你可以用在咖喱的表达方式上,比如:
[1,2,3].remove(2).sort()