如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
尝试使用过滤器方法使用此代码, 您可以从数组中删除任何特定项 。
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];
function removeItem(arr, value) {
return arr.filter(function (ele) {
return ele !== value;
});
}
console.log(removeItem(arr, 6));
其他回答
Vanilla JavaScript(ES5.1) - 已建立版本
浏览器支持: Internet Explorer 9 或后( 详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Modifies the array “in place”, i.e. the array passed as an argument
* is modified as opposed to creating a new array. Also returns the modified
* array for your convenience.
*/
function removeInPlace(array, item) {
var foundIndex, fromIndex;
// Look for the item (the item can have multiple indices)
fromIndex = array.length - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item (in place)
array.splice(foundIndex, 1);
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex);
}
// Return the modified array
return array;
}
Vanilla JavaScript(ES5.1) - 永恒版本
浏览器支持: 与原版的香草 JavaScript 相同
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
var arrayCopy;
arrayCopy = array.slice();
return removeInPlace(arrayCopy, item);
}
Vanilla ES6 - 永恒版本
浏览器支持: Chrome 46, 边缘 12, Firefox 16, Opera 37, Safari 8 (详细浏览器支持)
/**
* Removes all occurences of the item from the array.
*
* Returns a new array with all the items of the original array except
* the specified item.
*/
function remove(array, item) {
// Copy the array
array = [...array];
// Look for the item (the item can have multiple indices)
let fromIndex = array.length - 1;
let foundIndex = array.lastIndexOf(item, fromIndex);
while (foundIndex !== -1) {
// Remove the item by generating a new array without it
array = [
...array.slice(0, foundIndex),
...array.slice(foundIndex + 1),
];
// Bookkeeping
fromIndex = foundIndex - 1;
foundIndex = array.lastIndexOf(item, fromIndex)
}
// Return the new array
return array;
}
虽然大多数先前的答复都回答了这个问题,但是为什么没有使用切片()方法还不够清楚。 是的,过滤器()符合不可改变的标准,但做以下更短的等值如何?
const myArray = [1,2,3,4];
现在让我们说我们应该从阵列中删除第二个元素, 我们可以简单地做到:
const newArray = myArray.slice(0, 1).concat(myArray.slice(2, 4));
// [1,3,4]
今天,社区强烈鼓励从阵列中删除元素,因为其简单和不可改变的性质。 一般来说, 导致突变的方法应该避免。 例如, 鼓励您将推( ) 替换为 concat () , 并用切片 () 替换为 plus () 。
不需要使用 indexof 或 spolice 。 但是, 如果您只想要删除一个元素的发生, 它的效果会更好 。
查找并移动( 移动) :
function move(arr, val) {
var j = 0;
for (var i = 0, l = arr.length; i < l; i++) {
if (arr[i] !== val) {
arr[j++] = arr[i];
}
}
arr.length = j;
}
使用索引和串点( 索引) :
function indexof(arr, val) {
var i;
while ((i = arr.indexOf(val)) != -1) {
arr.splice(i, 1);
}
}
只使用复数( 复数) :
function splice(arr, val) {
for (var i = arr.length; i--;) {
if (arr[i] === val) {
arr.splice(i, 1);
}
}
}
带有 1000 元素的阵列( 平均超过 10,000 次运行) 的节点js 上的运行时间 :
指数比移动要慢10倍左右。 即使通过删除对 Enterof 的调用来改进指数,它的表现也比移动差得多。
Remove all occurrences:
move 0.0048 ms
indexof 0.0463 ms
splice 0.0359 ms
Remove first occurrence:
move_one 0.0041 ms
indexof_one 0.0021 ms
const arr = [1, 2, 3, 4, 5]
console.log(arr) // [ 1, 2, 3, 4, 5 ]
假设你想从Arr删除3号
const newArr = arr.filter(w => w !==3)
console.log(newArr) // [ 1, 2, 4, 5 ]
调
2021年更新
您的问题是如何从数组中删除一个特定项目。 您在具体项目中指的是一个数字, 例如 。 从数组中删除数字 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 函数。 它会对所有数组项目进行循环, 并只返回符合箭头函数的项目。 因此, 旧数组保持不变, 而称为新数组的结果包含不等于5的所有项目。 您可以自己在网上测试它 。
您可以将数组. prototype. filter like this:
调
考虑考虑的考虑
守则质量
ARray. prototype. filter 是最容易读取的方法来删除这个例子中的数字, 它给错误留下很少的位置, 并使用联署材料的核心功能 。
为什么不设置数组. prototype.map?
ray. prototype.map 有时被视为用于此用途的数组. prototype. filter 的替代选项。 但不应使用它。 原因是数组. prototype. filter 概念上用来过滤符合箭头函数( 我们需要的精度)的项目, 而数组. prototype.map 则用来转换项目。 由于在对项目进行迭接时我们不修改项目, 正确的功能是数组. prototype. filter 。
支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助支助
从今天(11.4.2022)起,94.08%的互联网用户浏览器支持阵列. prototype. filter。 所以一般地说, 使用是安全的。 但是, IE6 - 8 不支持它。 所以, 如果您的用法案例需要支持这些浏览器, Chris Ferdinanti 做了一个不错的多填。
业绩 业绩业绩 业绩业绩
Array. prototype. filter 对大多数使用的案例来说都是很好的。 但是, 如果您在寻找高级数据处理的性能改进, 您可以探索一些其他选项, 比如使用纯的选项。 另一个伟大的选项是重新思考您正在处理的阵列是否真的必须如此大。 这或许是一个信号, 即 JavaScript 应该从数据源获得一个减少的处理阵列 。
不同可能性的基准:https://jsben.ch/C5MXz