如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
使用 JavaScript 原型特性定义列对象上名为删除() 的方法 。
使用使用复数()满足要求的方法。
请看看下面的代码
Array.prototype.remove = function(item) {
// 'index' will have -1 if 'item' does not exist,
// else it will have the index of the first item found in the array
var index = this.indexOf(item);
if (index > -1) {
// The splice() method is used to add/remove items(s) in the array
this.splice(index, 1);
}
return index;
}
var arr = [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
// Printing array
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)
// Removing 67 (getting its index, i.e. 2)
console.log("Removing 67")
var index = arr.remove(67)
if (index > 0){
console.log("Item 67 found at ", index)
} else {
console.log("Item 67 does not exist in array")
}
// Printing updated array
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4];
console.log(arr)
// ............... Output ................................
// [ 11, 22, 67, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]
// Removing 67
// Item 67 found at 2
// [ 11, 22, 45, 61, 89, 34, 12, 7, 8, 3, -1, -4 ]
注:下面是用完整示例代码执行的完整示例代码。REPL编号js REPL来描述推 ()、 流行 ()、 转移 ()、 非转移 () 和 组合 () 方法的使用 。
> // Defining an array
undefined
> var arr = [12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34];
undefined
> // Getting length of array
undefined
> arr.length;
16
> // Adding 1 more item at the end i.e. pushing an item
undefined
> arr.push(55);
17
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34, 55 ]
> // Popping item from array (i.e. from end)
undefined
> arr.pop()
55
> arr
[ 12, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Remove item from beginning
undefined
> arr.shift()
12
> arr
[ 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> // Add item(s) at beginning
undefined
> arr.unshift(67); // Add 67 at beginning of the array and return number of items in updated/new array
16
> arr
[ 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.unshift(11, 22); // Adding 2 more items at the beginning of array
18
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> // Define a method on array (temporarily) to remove an item and return the index of removed item; if it is found else return -1
undefined
> Array.prototype.remove = function(item) {
... var index = this.indexOf(item);
... if (index > -1) {
..... this.splice(index, 1); // splice() method is used to add/remove items in array
..... }
... return index;
... }
[Function]
>
> arr
[ 11, 22, 67, 45, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(45); // Remove 45 (you will get the index of removed item)
3
> arr
[ 11, 22, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(22) // Remove 22
1
> arr
[ 11, 67, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
> arr.remove(67) // Remove 67
1
> arr
[ 11, 67, 89, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(89) // Remove 89
2
> arr
[ 11, 67, 34, 12, 7, 8, 3, -1, -4, -11, 0, 56, 12, 34 ]
>
> arr.remove(100); // 100 doesn't exist, remove() will return -1
-1
>
其他回答
Array.prototype.removeByValue = function (val) {
for (var i = 0; i < this.length; i++) {
if (this[i] === val) {
this.splice(i, 1);
i--;
}
}
return this;
}
var fruits = ['apple', 'banana', 'carrot', 'orange'];
fruits.removeByValue('banana');
console.log(fruits);
// -> ['apple', 'carrot', 'orange']
大部分答案都用...
for loop
虽然所有解决办法都应采用这些方法,但我认为我们可以使用字符串操作.
需要注意的关于这一解决办法的要点 -- --
诀窍是...
stringify
数据集和搜索值split
分隔符上的数据,
. remove = (input, value) => {
const stringVal = JSON.stringify(value);
const result = JSON.stringify(input)
return result.replace(stringVal, "").split(",");
}
这里创建了一个测试对象和数字 JSF 的 JSF 。 。 。https://jsfiddle.net/4t7zhkce/33/
检查remove
方法在小提琴中。
今天(2019-12-09),我为选择的解决方案对macOS v10.13.6(高塞拉利昂)进行性能测试。delete
(A),但与其他方法相比,我并不使用它,因为它在数组中留下了空白。
结 结 结 结 结 结 结
array.splice
(C) (小型阵列有第二次的Safeare除外)array.slice+splice
(H) 是Firefox和Safari最快、不可改变的解决办法;Array.from
(B) 铬含量最快在测试中,我以不同的方式从数组中删除中间元素。A、C本地的解决方案。B、D、E、F、G、H解决方案是不可改变的。
包含 10 元素的阵列结果
在铬array.splice
(C) 是当地最快的解决办法。array.filter
(D) 是最快、不可改变的解决方案。array.slice
(F). 您可以在机器上进行测试在这里.
带有 1 000 000 元素的阵列结果
在铬array.splice
(C) 是最迅速的就地解决办法delete
(C) 的快速性相似,但它在数组中留下了一个空位(因此它不执行“完全移除”)。array.slice-splice
(H) 是最快的不可改变的解决方案。 最慢的是array.filter
(D和E). 您可以在您的机器上进行测试。在这里.
var a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var log = (letter,array) => console.log(letter, array.join `,`);
function A(array) {
var index = array.indexOf(5);
delete array[index];
log('A', array);
}
function B(array) {
var index = array.indexOf(5);
var arr = Array.from(array);
arr.splice(index, 1)
log('B', arr);
}
function C(array) {
var index = array.indexOf(5);
array.splice(index, 1);
log('C', array);
}
function D(array) {
var arr = array.filter(item => item !== 5)
log('D', arr);
}
function E(array) {
var index = array.indexOf(5);
var arr = array.filter((item, i) => i !== index)
log('E', arr);
}
function F(array) {
var index = array.indexOf(5);
var arr = array.slice(0, index).concat(array.slice(index + 1))
log('F', arr);
}
function G(array) {
var index = array.indexOf(5);
var arr = [...array.slice(0, index), ...array.slice(index + 1)]
log('G', arr);
}
function H(array) {
var index = array.indexOf(5);
var arr = array.slice(0);
arr.splice(index, 1);
log('H', arr);
}
A([...a]);
B([...a]);
C([...a]);
D([...a]);
E([...a]);
F([...a]);
G([...a]);
H([...a]);
This snippet only presents code used in performance tests - it does not perform tests itself.
浏览器比较:Chrome v78.0.0,Safari v.13.0.4和Firefox v71.0.0
您的问题是如何从数组中删除一个特定项目。 您在具体项目中指的是一个数字, 例如 。 从数组中删除数字 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
有许多方法可以删除指定元素从 JavaScript 阵列中得出的。 以下是我研究中可以想到的五种最佳方法。
1. 直接使用“ splice () ” 方法
在以下代码段,从数组中删除特定预定位置的元素。
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Original array: " + arr);
var removed = arr.splice(4, 2);
console.log("Modified array: " + arr);
console.log("Elements removed: " + removed);
2. 使用“ splice() () ” 方法用“ valice () ” 方法用“ value () ” 删除元素
在以下代码段的以下代码段中,如果条件在用于循环。
var arr = [1, 2, 6, 3, 2, 6, 7, 8, 9, 10];
console.log("Original array: " + arr);
for (var i = 0; i < arr.length; i++) {
if (arr[i] === 6) {
var removed = arr.splice(i, 1);
i--;
}
}
console.log("Modified array: " + arr); // 6 is removed
console.log("Removed elements: " + removed);
3. 使用“过滤器()”方法删除按值选择的元素
与使用“ spice() () ” 方法的执行类似, 但它没有改变现有的数组, 而是创造了新的元素阵列, 从而删除了不想要的元素 。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
var filtered = array.filter(function(value, index, arr) {
return value != 6 ;
});
console.log("Original array: "+array);
console.log("New array created: "+filtered); // 6 is removed
4. 在“ Lodash” JavaScript 库中使用“ remove () ” 方法
在下面的代码段中, JavaScript 库里有叫作“ Lodash ” 的删除() 方法。 这个方法也与过滤方法相似 。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
console.log("Original array: " + array);
var removeElement = _.remove(array, function(n) {
return n === 6;
});
console.log("Modified array: " + array);
console.log("Removed elements: " + removeElement); // 6 is removed
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
5. 制作自定义除去方法
JavaScript没有本地的“ array. remove” 方法, 但我们可以创建一种使用以上方法的方法, 用于在以下代码片段中执行 。
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
function arrayRemove(arr, value) {
return arr.filter(function(element) {
return element != value;
});
}
console.log("Original array: " + array);
console.log("Modified array: " + arrayRemove(array, 6)); // 6 is removed
最后方法(第5项)更适合解决上述问题。