如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
spolice () 函数能够将数组中的项目还给您, 从特定的索引中删除项目 / 项 :
函数删除 ArrayText( index, 数组) { 数组. splice( index, 1); 返回数组; } 让数组 = [ 1, 2, 3,4]; 让 index = 2; 数组 = 删除 ArrayText( index, 数组); 控制台. log( 数组);
其他回答
有许多方法可以从 JavaScript 阵列中去除一个特定元素。 下面是我研究中可以找到的五种最佳方法。
1. 直接使用“ splice () ” 方法
在以下代码段,从数组中删除特定预定位置的元素。
语法: 数组_ name.spice( begin_ index, 数字_ elements_ remove) ; 应用程序 :
var arr = [1、2、3、4、5、6、7、8、9、10]; 控制台.log ("原件阵列: "+arr " ; var remote = ar.spice(4、2); 控制台.log ("修改后的阵列: "+arr " ; 控制台.log ("元素删除: "+删除 " );
2. 使用“ splice() () ” 方法用“ valice () ” 方法用“ value () ” 删除元素
在以下代码段中,我们可以使用循环内的条件,删除所有等于预先确定值(前:所有元素等于值6)的所有元素。
var arr = [1, 2, 6, 3, 2, 6, 7, 8, 8, 9, 10] 控制台.log ("原件阵列: "+arr”; 对于 (var i = 0; i < arr. 长度; i++) { { {var remove = rr.spice(i, 1); i-; }} 控制台. log ("修改阵列: "+arr " ); / / 6 被移除控制台. log ("重新删除的元素: "+删除 ");
3. 使用“过滤器()”方法删除按值选择的元素
与使用“ spice() () ” 方法的执行类似, 但它没有改变现有的数组, 而是创造了新的元素阵列, 从而删除了不想要的元素 。
var 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 9, 10] ; var 过滤 = 数组. filter( 函数( 值, 索引, rr) { 返回值 = 6 ; } ; 控制台. log ("原件数组 : "+array" ); 控制台. log ("新建数组 : "+filtered" ; / 6 被删除
4. 在“ Lodash” JavaScript 库中使用“ remove () ” 方法
在下面的代码段中, JavaScript 库里有叫作“ Lodash ” 的删除() 方法。 这个方法也与过滤方法相似 。
var 数组 = [1, 2, 3, 4, 5, 6, 7, 7, 8, 9, 10] 控制台. log ("原件数组: "+数组: "+数组: "+ + + + 数组: "; var remote Eplement = _.remove (ary, 函数(n) { return n\\ 6; }) 控制台. log ("修改后的数组: "+ 数组: "+ 数组: "removed 元素: "+ removeElement 元素 " ); / / 6 被删除 < src=" https://cdn.jsdelivr.net/npm/lodash@4.17.21lodash.min.js" / stamp >
5. 制作自定义除去方法
JavaScript没有本地的“ array. remove” 方法, 但我们可以创建一种使用以上方法的方法, 用于在以下代码片段中执行 。
var 数组 = [1, 2, 3, 4, 5, 6, 7, 8, 8, 9, 10] ; 函数数组 remove( arve, value) { 返回 arr. filter( 函数( 元素) { 返回元素 $= = 值;} } 控制台. log (" 原件数组: "+ 数组 " ); 控制台. log (" 修改数组: "+ trightremove( 数组, 6) ); / / 6 被删除 。
最后方法(第5项)更适合解决上述问题。
多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。
我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?
纠正我,如果我错了, 但我们不能假设在关键值地图中, JavaScript 物体的种类就是 JavaScript 对象, 关键检索机制可以被假定为高度工程化和优化? (NB: 如果一些超级专家告诉我情况并非如此, 我建议使用ECMAScript 6 的地图类, 这当然会是这样 ) 。
我只是建议,在某些情况下,最好的解决方案可能是将您的阵列转换为对象... 当然,问题在于您可能有重复的整数值。 我建议将那些放在桶中作为关键 {} {} 值条目中的“ 价值” 部分。 (NB: 如果您确定您没有重复的阵列元素, 这样可以简单得多 : 值“ 和” 键, 只需去对象. values (...) 就可以返回修改后的阵列 。)
所以,你可以做到这一点:
const arr = [ 1, 2, 55, 3, 2, 4, 55 ];
const f = function( acc, val, currIndex ){
// We have not seen this value before: make a bucket... NB: although val's typeof is 'number',
// there is seamless equivalence between the object key (always string)
// and this variable val.
! ( val in acc ) ? acc[ val ] = []: 0;
// Drop another array index in the bucket
acc[ val ].push( currIndex );
return acc;
}
const myIntsMapObj = arr.reduce( f, {});
console.log( myIntsMapObj );
产出:
对象 [<1个空位>、Array1、Array[2]、Array1、Array1、<5个空位]、46个以上...]
然后很容易删除所有55个数字。
delete myIntsMapObj[ 55 ]; // Again, although keys are strings this works
你不必全部删除: 指数值按外观被挤进桶里, 所以(例如):
myIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();
将分别删除第一次和最后一次发生的情况。 您可以很容易地计算发生频率, 将一个桶的内装物转移到另一个桶等, 将所有55个都替换为3个 。
从您的“ bucket 对象” 中获取修改的整数组略为涉及, 但数量不多 : 每个桶都包含以( 字符串) 键表示的值的索引( 原始数组) 。 这些桶的每一个值也是独一无二的( 每个是原始数组中唯一的索引值 ) : 所以您把它们转换成新对象中的键, 将“ 整数” 中的( 真实) 整数作为值... 然后对键进行排序, 然后对对象. values (...) 排序 。
这听起来很牵强,很费时... 但显然一切都取决于环境 和理想的用法。我的理解是 JavaScript 的所有版本和背景 只能用一条线运作, 这条线不会“放手 ” , 所以可能会有一些可怕的拥堵 , 使用“强力” 方法: 不是由 Enterof 行动造成的, 而是由多个重复的切片/碎块操作造成的。
增编 如果你确定这对你来说太过分工程了 使用这个案子的话 最简单的"强力"方法肯定就是
const arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )
(是的,其他的答案 已经发现阵列. 原型. 过滤器... )
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');
Array.prototype.remove = function(x) {
var y=this.slice(x+1);
var z=[];
for(i=0;i<=x-1;i++) {
z[z.length] = this[i];
}
for(i=0;i<y.length;i++){
z[z.length]=y[i];
}
return z;
}
更新: 只有当您无法使用 ECMAScript 2015 (前称ES6) 时, 才会推荐使用此方法。 如果您可以使用此方法, 其它答案可以提供更清晰的落实 。
此条格将解决您的问题, 并删除所有参数, 而不是仅一个( 或指定值 ) 。
Array.prototype.destroy = function(obj){
// Return null if no objects were found and removed
var destroyed = null;
for(var i = 0; i < this.length; i++){
// Use while-loop to find adjacent equal objects
while(this[i] === obj){
// Remove this[i] and store it within destroyed
destroyed = this.splice(i, 1)[0];
}
}
return destroyed;
}
用法 :
var x = [1, 2, 3, 3, true, false, undefined, false];
x.destroy(3); // => 3
x.destroy(false); // => false
x; // => [1, 2, true, undefined]
x.destroy(true); // => true
x.destroy(undefined); // => undefined
x; // => [1, 2]
x.destroy(3); // => null
x; // => [1, 2]