如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
如何从数组中删除一个特定值? 类似 :
array.remove(value);
我必须使用核心核心核心JavaScript。 不允许框架 。
当前回答
最简单的方法可能是使用过滤功能。例如:
let array = ["hello", "world"]
let newarray = array.filter(item => item !== "hello");
console.log(newarray);
// ["world"]
其他回答
不可改变和一班制方式:
const newArr = targetArr.filter(e => e !== elementToDelete);
多可惜,您有数组整数,而不是键是这些整数的字符串等量的对象。
我看过很多这样的答案, 在我看来,它们似乎都使用了“粗力 ” ( 粗力 ) 。 我还没有检查每个答案, 如果不是这样的话, 请道歉。 对于一个小的阵列来说,这很好, 但如果你有千个整数呢?
纠正我,如果我错了, 但我们不能假设,在key => value
JavaScript 对象的地图, 即 JavaScript 对象的地图, 关键检索机制可以被假定为高度工程设计和优化吗? (NB:如果一些超级专家告诉我情况并非如此, 我可以建议使用ECMAScript 6's映图类相反,这当然会是) )。
我只是建议,在某些情况下,最好的解决办法可能是 将你的阵列转换成一个物体... 问题当然是,你可能会重复整数值。我建议把这些放在桶中作为“价值”的一部分。key => value
。 (NB: 如果您确定您没有重复的数组元素, 这样可以简单得多 : 值“ 和” 键, 然后直接去Object.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 );
产出:
然后很容易删除所有55个数字。
delete myIntsMapObj[ 55 ]; // Again, although keys are strings this works
你不必全部删除: 指数值按外观被挤进桶里, 所以(例如):
myIntsMapObj[ 55 ].shift(); // And
myIntsMapObj[ 55 ].pop();
将分别删除第一次和最后一次发生的情况。 您可以很容易地计算发生频率, 将一个桶的内装物转移到另一个桶等, 将所有55个都替换为3个 。
获取已修改的int
从您的“ bucket 对象” 的数组数组中略微涉及到“ bucket 对象” ,但数量不多:每个桶都包含( 最初数组) 所代表的数值的索引( 以原始数组表示) 。string
关键值 。 这些桶的每个值也是独特的(每个都是独一的)指数指数值原数组中原始数组 : 因此, 您可以在新对象中将其转换为键, 以“ 整数字符串键” 中的( 真实) 整数作为值... 然后排序键然后去Object.values( ... )
.
这听起来很牵扯,很费时... 但显然一切都取决于环境 和理想的用法。我的理解是 JavaScript 的所有版本和背景 只能用一条线运作, 这条线不会“放手 ” , 所以可能会有一些可怕的堵塞 与“强力 ” 方法: 原因不在于它。indexOf
选项,但多次重复slice
/splice
行动。
Addendum如果你是确定这对你的使用来说太过于工程化了 当然最简单的"强力"方法就是
const arr = [ 1, 2, 3, 66, 8, 2, 3, 2 ];
const newArray = arr.filter( number => number !== 3 );
console.log( newArray )
(是的,其他答案已发现)Array.prototype.filter
...)
使用过滤过滤器这是达到这一要求的优雅方式。filter
将不改变原始数组。
const num = 3;
let arr = [1, 2, 3, 4];
const arr2 = arr.filter(x => x !== num);
console.log(arr); // [1, 2, 3, 4]
console.log(arr2); // [1, 2, 4]
您可以使用filter
然后将结果指定为原始数组,如果你想实现突变清除行为的话。
const num = 3;
let arr = [1, 2, 3, 4];
arr = arr.filter(x => x !== num);
console.log(arr); // [1, 2, 4]
顺便说一句,filter
将删除在条件中匹配的所有事件( 不只是第一次发生) , 正如您在下面的示例中看到的一样
const num = 3;
let arr = [1, 2, 3, 3, 3, 4];
arr = arr.filter(x => x !== num);
console.log(arr); // [1, 2, 4]
万一您只想删除第一种情况,您可以使用复盘方法
const num = 3;
let arr = [1, 2, 3, 3, 3, 4];
const idx = arr.indexOf(num);
arr.splice(idx, idx !== -1 ? 1 : 0);
console.log(arr); // [1, 2, 3, 3, 4]
检查一下这个代码 每个代码都有效主浏览器.
remove_item = function(arr, value) {
var b = '';
for (b in arr) {
if (arr[b] === value) {
arr.splice(b, 1);
break;
}
}
return arr;
};
var array = [1,3,5,6,5,9,5,3,55]
var res = remove_item(array,5);
console.log(res)
更新 :只有当您无法使用 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]