如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
我找到了这个博客文章,
9 从 JavaScript 阵列中删除元素的方法 - 附加如何安全清除 JavaScript 阵列
我更喜欢用filter()
:
var filtered_arr = arr.filter(function(ele){
return ele != value;
})
其他回答
此函数从特定位置的数组中删除元素。
array.remove(position);
Array.prototype.remove = function (pos) {
this.splice(pos, 1);
}
var arr = ["a", "b", "c", "d", "e"];
arr.remove(2); // remove "c"
console.log(arr);
如果您不知道要删除的项目的位置, 请使用这个 :
array.erase(element);
Array.prototype.erase = function(el) {
let p = this.indexOf(el); // indexOf use strict equality (===)
if(p != -1) {
this.splice(p, 1);
}
}
var arr = ["a", "b", "c", "d", "e"];
arr.erase("c");
console.log(arr);
(function removeFromArrayPolyfill() {
if (window.Array.prototype.remove) return;
Array.prototype.remove = function (value) {
if (!this.length || !value) return;
const indexOfValue = this.indexOf(value);
if (indexOfValue >= 0) {
this.splice(indexOfValue, 1);
}
};
})();
// testing polyfill
const nums = [10, 20, 30];
nums.remove(20);
console.log(nums);//[10,30]
您的问题是如何从数组中删除一个特定项目。 您在具体项目中指的是一个数字, 例如 。 从数组中删除数字 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
您可以用所有存取器示例创建索引 :
<div >
</div>
function getIndex($id){
return (
this.removeIndex($id)
alert("This element was removed")
)
}
function removeIndex(){
const index = $id;
this.accesor.id.splice(index.id) // You can use splice for slice index on
// accessor id and return with message
}
<div>
<fromList>
<ul>
{...this.array.map( accesors => {
<li type="hidden"></li>
<li>{...accesors}</li>
})
}
</ul>
</fromList>
<form id="form" method="post">
<input id="{this.accesors.id}">
<input type="submit" callbackforApplySend...getIndex({this.accesors.id}) name="sendendform" value="removeIndex" >
</form>
</div>
如果数组含有重复的值,而您想要删除目标的所有发生点, 那么这就是前进的道路...
let data = [2, 5, 9, 2, 8, 5, 9, 5];
let target = 5;
data = data.filter(da => da !== target);
注: - 过滤器不会改变原始数组;相反它会创建一个新的数组。
所以再次分配很重要。
这又引出了另一个问题,你无法制造变数const
应该是:let
或var
.