如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
制约:我必须使用核心 JavaScript 。 框架不允许 。
当前回答
洛达什:
let a1 = {name:'a1'}
let a2 = {name:'a2'}
let a3 = {name:'a3'}
let list = [a1, a2, a3]
_.remove(list, a2)
//list now is [{name: "a1"}, {name: "a3"}]
请选中此项以获取细节 :. remove( 数组, [预测=. identity] )
其他回答
创建新阵列 :
var my_array = new Array();
添加元素到此数组 :
my_array.push("element1");
函数索引of (返回指数或 -1 未找到时) :
var indexOf = function(needle)
{
if (typeof Array.prototype.indexOf === 'function') // Newer browsers
{
indexOf = Array.prototype.indexOf;
}
else // Older browsers
{
indexOf = function(needle)
{
var index = -1;
for (var i = 0; i < this.length; i++)
{
if (this[i] === needle)
{
index = i;
break;
}
}
return index;
};
}
return indexOf.call(this, needle);
};
检查此元素的索引( 使用 Firefox 和 Internet Explorer 8 (及以后) 测试) :
var index = indexOf.call(my_array, "element1");
从数组中删除位于索引处的 1 元素
my_array.splice(index, 1);
Array. prototype. removeByValue = 函数( val) {, 对于 (var i = 0; i < this. long; i++) { 如果 (i) val) { this. splice (i, 1); i- ; } 返回此 ;} var falts = [apple, 'banana', 'carrot', 'orange' ] ; falves.remove ByValue ('banana'); 礼服. log (fruts); / / - > [apple, 'carrot', 'orange' ]
虽然大多数先前的答复都回答了这个问题,但是为什么没有使用切片()方法还不够清楚。 是的,过滤器()符合不可改变的标准,但做以下更短的等值如何?
const myArray = [1,2,3,4];
现在让我们说我们应该从阵列中删除第二个元素, 我们可以简单地做到:
const newArray = myArray.slice(0, 1).concat(myArray.slice(2, 4));
// [1,3,4]
今天,社区强烈鼓励从阵列中删除元素,因为其简单和不可改变的性质。 一般来说, 导致突变的方法应该避免。 例如, 鼓励您将推( ) 替换为 concat () , 并用切片 () 替换为 plus () 。
如果您必须支持旧版的 Internet Explorer , 我建议使用以下多个填充( 注意: 这不是一个框架 ) 。 这是所有现代阵列方法( JavaScript 1. 8. 5/ ECMAScript 5 Array Extras) 的100%的后向兼容替换, 用于 Internet Explorer 6+, Firefox 1. 5+, Chrome, Safari, & Opera 。
https://github.com/pludude/array-generics https://github.com/pludude/ array-generics https://github.com/plusdude/arary-generics/servics/
多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。
我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?
纠正我,如果我错了, 但我们不能假设在关键值地图中, 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 )
(是的,其他的答案 已经发现阵列. 原型. 过滤器... )