如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
您可以为此创建一个原型。只需通过数组元素和您想要从数组元素中删除的值:
Array.prototype.removeItem = function(array,val) {
array.forEach((arrayItem,index) => {
if (arrayItem == val) {
array.splice(index, 1);
}
});
return array;
}
var DummyArray = [1, 2, 3, 4, 5, 6];
console.log(DummyArray.removeItem(DummyArray, 3));
其他回答
您的问题是如何从数组中删除一个特定项目。 您在具体项目中指的是一个数字, 例如 。 从数组中删除数字 5 。 据我了解, 您正在寻找类似 :
// PSEUDOCODE, SCROLL FOR COPY-PASTE CODE
[1,2,3,4,5,6,8,5].remove(5) // result: [1,2,3,4,6,8]
至于2021年,实现该目标的最佳途径是使用数组过滤功能:
const input = [1,2,3,4,5,6,8,5];
const removeNumber = 5;
const result = input.filter(
item => item != removeNumber
);
以上例子的使用数组. prototype. filter函数。它会对所有数组项目进行迭代,并且只返回符合箭头函数结果,旧阵列保持不变,而一个新的阵列则被称为result
包含不等于 5 的所有项目。您可以自己测试它在线在线.
你可以直观地看到数组. prototype. filter像这样 :
守则质量
Array.prototype.filter
在此情况下,这是消除数字的最容易读懂的方法,对错误几乎没有留有余地,并使用联署材料的核心功能。
为什么不array.prototype.map
?
Array.prototype.map
有时被视作一种替代array.prototype.filter
对于此用途的话, 使用它。 但不应该使用它。 原因是数组. prototype. filter概念上用于过滤过滤项目能够满足箭头函数(精确我们需要的)的箭头功能,而数组. prototype.map用于变换项。由于在对项目进行循环时,我们不更改项目,使用的适当功能是array.prototype.filter
.
支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助
截至今天(11.4.2022)94.08%的互联网用户'浏览器支持iE6 - 8 不支持它。 所以, 如果您的使用案例需要支持这些浏览器, 将会有一个不错的多元填充克里斯·费迪南蒂的作品
业绩 业绩业绩 业绩业绩
Array.prototype.filter
对于大多数使用过的个案来说是巨大的。然而,如果您在寻找先进数据处理的性能改进,您可以探索一些高级数据处理的性能改进。其它选项像使用纯for
。另一个伟大的选项是重新思考您正在处理的数组是否真的必须如此大。这可能是一个信号,即 JavaScript 应该从数据源获得一个减少的数组来进行处理。
不同可能性的基准:https://jsben.ch/C5MXz
我刚刚创建了一个多填充Array.prototype
通过Object.defineProperty
以删除数组中一个想要的元素,而不会在稍后通过for .. in ..
if (!Array.prototype.remove) {
// Object.definedProperty is used here to avoid problems when iterating with "for .. in .." in Arrays
// https://stackoverflow.com/questions/948358/adding-custom-functions-into-array-prototype
Object.defineProperty(Array.prototype, 'remove', {
value: function () {
if (this == null) {
throw new TypeError('Array.prototype.remove called on null or undefined')
}
for (var i = 0; i < arguments.length; i++) {
if (typeof arguments[i] === 'object') {
if (Object.keys(arguments[i]).length > 1) {
throw new Error('This method does not support more than one key:value pair per object on the arguments')
}
var keyToCompare = Object.keys(arguments[i])[0]
for (var j = 0; j < this.length; j++) {
if (this[j][keyToCompare] === arguments[i][keyToCompare]) {
this.splice(j, 1)
break
}
}
} else {
var index = this.indexOf(arguments[i])
if (index !== -1) {
this.splice(index, 1)
}
}
}
return this
}
})
} else {
var errorMessage = 'DANGER ALERT! Array.prototype.remove has already been defined on this browser. '
errorMessage += 'This may lead to unwanted results when remove() is executed.'
console.log(errorMessage)
}
删除整数值
var a = [1, 2, 3]
a.remove(2)
a // Output => [1, 3]
删除字符串值
var a = ['a', 'ab', 'abc']
a.remove('abc')
a // Output => ['a', 'ab']
删除布尔值
var a = [true, false, true]
a.remove(false)
a // Output => [true, true]
也可以通过此方法从数组中移除对象Array.prototype.remove
方法。只需指定key => value
of the Object
您想要删除。
删除对象值
var a = [{a: 1, b: 2}, {a: 2, b: 2}, {a: 3, b: 2}]
a.remove({a: 1})
a // Output => [{a: 2, b: 2}, {a: 3, b: 2}]
Array.prototype.removeItem = function(a) {
for (i = 0; i < this.length; i++) {
if (this[i] == a) {
for (i2 = i; i2 < this.length - 1; i2++) {
this[i2] = this[i2 + 1];
}
this.length = this.length - 1
return;
}
}
}
var recentMovies = ['Iron Man', 'Batman', 'Superman', 'Spiderman'];
recentMovies.removeItem('Superman');
您在数组中有 1 到 9 个, 您想要删除 5 个 。 请使用以下代码 :
var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var newNumberArray = numberArray.filter(m => {
return m !== 5;
});
console.log("new Array, 5 removed", newNumberArray);
如果您想要多个值。例如:- 1,7,8
var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var newNumberArray = numberArray.filter(m => {
return (m !== 1) && (m !== 7) && (m !== 8);
});
console.log("new Array, 1,7 and 8 removed", newNumberArray);
如果您想要删除数组中的数组值。 例如 : [3,4,5]
var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9];
var removebleArray = [3,4,5];
var newNumberArray = numberArray.filter(m => {
return !removebleArray.includes(m);
});
console.log("new Array, [3,4,5] removed", newNumberArray);
包括支持的浏览器链接链接.
非就地解决办法
arr.slice(0,i).concat(arr.slice(i+1));
let arr = [10, 20, 30, 40, 50]
let i = 2 ; // position to remove (starting from 0)
let r = arr.slice(0,i).concat(arr.slice(i+1));
console.log(r);