如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
今天(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
其他回答
这提供了一个前提值, 而不是一个值 。
注:这将更新给定的数组, 并返回受影响的行 。
var removed = helper.remove(arr, row => row.id === 5 );
var removed = helper.removeAll(arr, row => row.name.startsWith('BMW'));
var helper = {
// Remove and return the first occurrence
remove: function(array, predicate) {
for (var i = 0; i < array.length; i++) {
if (predicate(array[i])) {
return array.splice(i, 1);
}
}
},
// Remove and return all occurrences
removeAll: function(array, predicate) {
var removed = [];
for (var i = 0; i < array.length; ) {
if (predicate(array[i])) {
removed.push(array.splice(i, 1));
continue;
}
i++;
}
return removed;
},
};
减少方法的利润如下:
(a) 需要按索引删除某一要素:
function remove(arr, index) {
return arr.reduce((prev, x, i) => prev.concat(i !== index ? [x] : []), []);
}
b) 需要删除元素值(int)的元素:
function remove(arr, value) {
return arr.reduce((prev, x, i) => prev.concat(x !== value ? [x] : []), []);
}
这样我们就可以返回一个新的阵列( 将会以酷酷的功能方式- 比使用推或组合要好得多) , 并删除元素 。
我本人也有这个问题(在更换阵列是可以接受的情况下),
var filteredItems = this.items.filter(function (i) {
return i !== item;
});
要给上面的片段略加上下文:
self.thingWithItems = {
items: [],
removeItem: function (item) {
var filteredItems = this.items.filter(function (i) {
return i !== item;
});
this.items = filteredItems;
}
};
此解决方案应该同时使用引用项和值项。 它都取决于您是否需要保持对原始数组的引用, 以判断该解决方案是否适用 。
对我而言,越简单越好,2018年(2019年左右),我给你这个(近一点)单行话,回答最初的问题:
Array.prototype.remove = function (value) {
return this.filter(f => f != value)
}
有用的是,你可以用在咖喱的表达方式上,比如:
[1,2,3].remove(2).sort()
我喜欢这个单行道:
arr.includes(val) && arr.splice(arr.indexOf(val), 1)
null
或undefined
作为原型
// remove by value. return true if value found and removed, false otherwise
Array.prototype.remove = function(val)
{
return this.includes(val) && !!this.splice(this.indexOf(val), 1);
}
(是的,我读了所有其他的答案 却找不到一个结合的答案)includes
和splice
在同一行中。 )